Example #1
0
File: request.c Project: dmt4/ne
int request_document(void) {

	int i = -1;
	req_list rl;
	buffer *b = (buffer *)buffers.head;

	if (b->b_node.next && req_list_init(&rl, NULL, true, true, '*')==OK) {
		i = 0;
		int cur_entry = 0;
		while(b->b_node.next) {
			if (b == cur_buffer) cur_entry = i;
			req_list_add(&rl, b->filename ? b->filename : UNNAMED_NAME, b->is_modified);
			b = (buffer *)b->b_node.next;
			i++;
		}
		rl.ignore_tab = true;
		req_list_finalize(&rl);
		print_message(info_msg[SELECT_DOC]);
		i = request_strings(&rl, cur_entry);
		reset_window();
		draw_status_bar();
		if (i >= 0 && rl.reordered) {
			/* We're going to cheat big time here. We have an array of pointers
				at rl.entries that's big enough to hold all the buffer pointers,
				and that's exactly what we're going to use it for now. */
			b = (buffer *)buffers.head;
			for (int j = 0; b->b_node.next; j++ ) {
				rl.entries[j] = (char *)b;
				b = (buffer *)b->b_node.next;
				rem(b->b_node.prev);
			}
			/* Ack! We're removed all our buffers! */
			for (int j = 0; j < rl.cur_entries; j++) {
				add_tail(&buffers, (node *)rl.entries[rl.orig_order[j]]);
			}
		}

		req_list_free(&rl);
	}

	return i;
}
Example #2
0
File: request.c Project: dmt4/ne
char *request_syntax() {
	char syn_dir_name[512];
	char *p;
	req_list rl;
	DIR *d;

	if (req_list_init(&rl, filenamecmp, false, false, '*') != OK) return NULL;
	if ((p = exists_prefs_dir()) && strlen(p) + 2 + strlen(SYNTAX_DIR) < sizeof syn_dir_name) {
		strcat(strcpy(syn_dir_name, p), SYNTAX_DIR);
		if (d = opendir(syn_dir_name)) {
			load_syntax_names(&rl,d,true);
			closedir(d);
		}
	}
	if ((p = exists_gprefs_dir()) && strlen(p) + 2 + strlen(SYNTAX_DIR) < sizeof syn_dir_name) {
		strcat(strcpy(syn_dir_name, p), SYNTAX_DIR);
		if (d = opendir(syn_dir_name)) {
			load_syntax_names(&rl,d,false);
			closedir(d);
		}
	}
	req_list_finalize(&rl);
	p = NULL;
	int result;
	if (rl.cur_entries && (result = request_strings(&rl, 0)) != ERROR) {
		char * const q = rl.entries[result >= 0 ? result : -result - 2];
		if (p = malloc(strlen(q)+3)) {
			strcpy(p,q);
			if (p[strlen(p)-1] == rl.suffix) p[strlen(p)-1] = '\0';
			if (result < 0) {
				memmove(p + 1, p, strlen(p) + 1);
				p[0] = '\0';
			}
		}
	}
	req_list_free(&rl);
	return p;
}
Example #3
0
File: request.c Project: dmt4/ne
char *request_files(const char * const filename, bool use_prefix) {

	char * const cur_dir_name = ne_getcwd(CUR_DIR_MAX_SIZE);
	if (!cur_dir_name) return NULL;

	char * const dir_name = str_dup(filename);
	if (dir_name) {
		int result = 0;
		char * const p = (char *)file_part(dir_name);
		if (p != dir_name) {
			*p = 0;
			result = chdir(tilde_expand(dir_name));
		}
		free(dir_name);
		if (result == -1) return NULL;
	}

	req_list rl;
	bool next_dir;
	char *result = NULL;
	do {
		next_dir = false;
		if (req_list_init(&rl, filenamecmp, true, false, '/') != OK) break;

		DIR * const d = opendir(CURDIR);
		if (d) {

			stop = false;
			for(struct dirent * de; !stop && (de = readdir(d)); ) {
				const bool is_dir = is_directory(de->d_name);
				if (use_prefix && !is_prefix(file_part(filename), de->d_name)) continue;
				if (!req_list_add(&rl, de->d_name, is_dir)) break;
			}

			req_list_finalize(&rl);

			if (rl.cur_entries) {
				/* qsort(rl.entries, rl.cur_entries, sizeof(char *), filenamecmpp); */

				const int t = request_strings(&rl, 0);
				if (t != ERROR) {
					char * const p = rl.entries[t >= 0 ? t : -t - 2];
					if (p[strlen(p) - 1] == '/' && t >= 0) {
						p[strlen(p) - 1] = 0;
						if (chdir(p)) alert();
						else use_prefix = false;
						next_dir = true;
					}
					else {
						result = ne_getcwd(CUR_DIR_MAX_SIZE + strlen(p) + 2);
						if (strcmp(result, "/")) strcat(result, "/");
						strcat(result, p);
						if (t < 0) {
							memmove(result + 1, result, strlen(result) + 1);
							result[0] = 0;
						}
					}
				}
			}

			closedir(d);
		}
		else alert();
		req_list_free(&rl);
	} while(next_dir);

	chdir(cur_dir_name);
	free(cur_dir_name);

	return result;
}
Example #4
0
/** Server code. Server manages a queue of tasks to be executed in
this phase. Each task in the queue is scheduled to be executed
once. When all the tasks complete execution, the server populates the
queue again and repeats the process. The server terminates when all
populating a task queue returns an empty queue. */
void server_code() {
  task_list_t tlist;
  proc_list_t plist;
  req_list_t rlist;
  task_t *ptr;
  const char *pname = "server_code";
  const int world_me = GA_Nodeid();
  double e1, e2, e3, e4, f1, f2, f3, f4, f5, f6, f7;
  double t_pop_task=0, t_sig_start=0, t_poll=0, t_loop=0, t_loop1=0, t_poll2=0; 
  proc_t *procs;
  int default_grp;
/*   double util_wallsec_(); */
  t_ptask = 0.0;

  default_grp = GA_Pgroup_get_default();
/*   fprintf(stderr, "%d: 1 %s\n", world_me, pname); */

  e1 = util_wallsec_();
  plist_init(&plist);
/*   fprintf(stderr, "%d: 2 %s\n", world_me, pname); */
  task_list_init(&tlist);
/*   fprintf(stderr, "%d: 3 %s\n", world_me, pname); */
  
  e2 = util_wallsec_();
  while(1) {
    f1 = util_wallsec_();
    task_list_reset(&tlist);  
/*     fprintf(stderr, "%d: 4 %s\n", world_me, pname); */
    populate_tasks_((Integer *)&tlist);
/*     fprintf(stderr, "%d: 5. ntodo=%d nrunning=%d ndone=%d %s\n", */
/* 	    world_me, tlist.ntodo, tlist.nrunning, tlist.ndone, pname); */
    req_list_init(&rlist, &tlist);
    f2 = util_wallsec_();
    t_pop_task += (f2-f1);

    if(tlist.ntodo == 0) break;

    f2 = util_wallsec_();
    while(tlist.ntodo>0) {
#warning "Simple server implementation. To be optimized"
      
/*       int nproc = tlist.todo->nproc; */

      f3 = util_wallsec_();
      if(1 /*(tlist.nrunning==0)*/) {
	while(tlist.ntodo>0 &&
	      (procs=plist_assign_procs(&plist, tlist.todo->nproc))!=NULL) {
/* 	  	  fprintf(stderr, "%d:: Signalling start of a task\n", GA_Nodeid()); */
	  signal_task_start(tlist.todo, tlist.todo->nproc, procs, &rlist);
	  tlist.todo->proc_list = procs;
	  task_mark_running(&tlist, tlist.todo);

/* 	  	  fprintf(stderr, "%d: Started a task. ntodo=%d nrunning=%d ndone=%d %s\n", */
/* 	  		  world_me, tlist.ntodo, tlist.nrunning, tlist.ndone, pname); */
	}
      }
      f4 = util_wallsec_();
      t_sig_start += (f4-f3);
      /*       fprintf(stderr, "Polling for task completion notifies\n"); */

      if((ptr = wait_completion(&tlist,&plist,&rlist))!=NULL) {
	plist_reclaim_procs(&plist, ptr->nproc, ptr->proc_list);
	ptr->proc_list = NULL;
	task_mark_done(&tlist, ptr);
/* 	fprintf(stderr, "%d: Completed a task. ntodo=%d nrunning=%d ndone=%d %s\n", */
/* 		world_me, tlist.ntodo, tlist.nrunning, tlist.ndone, pname); */
      }
      f5 = util_wallsec_();
      t_poll += (f5-f3);
    }

    f6 = util_wallsec_();
    t_loop1 += (f6-f1);
    while(tlist.nrunning > 0) {
      if((ptr = wait_completion(&tlist, &plist,&rlist)) != NULL) {
	plist_reclaim_procs(&plist, ptr->nproc, ptr->proc_list);
	ptr->proc_list = NULL;
	task_mark_done(&tlist, ptr);
/*         fprintf(stderr, "%d: Completed a task. ntodo=%d nrunning=%d ndone=%d %s\n", */
/*                 world_me, tlist.ntodo, tlist.nrunning, tlist.ndone, pname); */
      }
    }
    req_list_destroy(&rlist);
    f7 = util_wallsec_();
    t_poll2 += (f7-f6);
    t_loop += (f7-f2);
  }
  e3 = util_wallsec_();
  plist_destroy(&plist);
  task_list_destroy(&tlist);
  /*  req_list_destroy(&rlist);*/
/*   fprintf(stderr, "%d:: Signalling termination\n", GA_Nodeid());  */
  signal_termination(SVR,GA_Pgroup_absolute_id(default_grp,SVR));
/*   fprintf(stderr, "%d:: Done signalling termination\n", GA_Nodeid());  */
  e4 = util_wallsec_();

/*   fprintf(stderr, "%d:: SERVER TOTAL time=%lf\n", ga_nodeid_(), e4-e1); */
/*   fprintf(stderr, "%d:: SERVER INIT time=%lf\n", ga_nodeid_(), e2-e1); */
/*   fprintf(stderr, "%d:: SERVER LOOP time=%lf\n", ga_nodeid_(), e3-e2); */
/*   fprintf(stderr, "%d:: SERVER TLOOP time=%lf\n", ga_nodeid_(), t_loop); */
/*   fprintf(stderr, "%d:: SERVER TLOOP1 time=%lf\n", ga_nodeid_(), t_loop1); */
/*   fprintf(stderr, "%d:: SERVER CLEANUP time=%lf\n", ga_nodeid_(), e4-e3); */
/*   fprintf(stderr, "%d:: SERVER POLLING time=%lf\n", ga_nodeid_(), t_poll); */
/*   fprintf(stderr, "%d:: SERVER POLLING2 time=%lf\n", ga_nodeid_(), t_poll2); */
/*   fprintf(stderr, "%d:: SERVER SIG_START time=%lf\n", ga_nodeid_(), t_sig_start); */
/*   fprintf(stderr, "%d:: SERVER POP_TASK time=%lf\n", ga_nodeid_(), t_pop_task); */
/*   fflush(stdout); */
}
Example #5
0
File: autocomp.c Project: vigna/ne
char *autocomplete(char *p, char *req_msg, const int ext, int * const error) {
	int max_len = 0, min_len = INT_MAX, prefix_len = strlen(p);

	assert(p);

	req_list_init(&rl, (cur_buffer->opt.case_search ? strcmp : strdictcmp), false, false, EXTERNAL_FLAG_CHAR);
	count_scanned = 0;

	search_buff(cur_buffer, p, cur_buffer->encoding, cur_buffer->opt.case_search, false);
	if (stop) {
		req_list_free(&rl);
		free(p);
		return NULL;
	}

	if (ext) {
		buffer *b = (buffer *)buffers.head;
		while (b->b_node.next) {
			if (b != cur_buffer) {
				search_buff(b, p, cur_buffer->encoding, cur_buffer->opt.case_search, true);
				if (stop) {
					req_list_free(&rl);
					free(p);
					return NULL;
				}
			}
			b = (buffer *)b->b_node.next;
		}
 	}

	for(int i = 0; i < rl.cur_entries; i++) {
		const int l = strlen(rl.entries[i]);
		if (max_len < l) max_len = l;
		if (min_len > l) min_len = l;
	}
	/* We compact the table into a vector of char pointers. */
	req_list_finalize(&rl);


	free(p);
	p = NULL;

#ifdef NE_TEST
	/* During tests, we always output the middle entry. */
	if (rl.cur_entries) {
		if (rl.entries[rl.cur_entries/2][strlen(rl.entries[rl.cur_entries/2]) - 1] == EXTERNAL_FLAG_CHAR) rl.entries[rl.cur_entries/2][strlen(rl.entries[rl.cur_entries/2]) - 1] = 0;
		p = str_dup(rl.entries[rl.cur_entries/2]);
	}
	*error = AUTOCOMPLETE_COMPLETED;
	req_list_free(&rl);
	return p;
#endif

	if (rl.cur_entries > 0) {
		qsort(rl.entries, rl.cur_entries, sizeof(char *), strdictcmpp);
		/* Find maximum common prefix. */
		int m = strlen(rl.entries[0]);
		if (rl.entries[0][m-1] == EXTERNAL_FLAG_CHAR) m--;
		for(int i = 1; i < rl.cur_entries; i++) {
			int j;
			for(j = 0; j < m; j++) 
				if (rl.entries[i][j] != rl.entries[0][j]) break;
			m = j;
		}

		/* If we can output more characters than the prefix len, we do so without
			starting the requester. */
		if (m > prefix_len) {
			p = malloc(m + 1);
			strncpy(p, rl.entries[0], m);
			p[m] = 0;
			*error = min_len == m ? AUTOCOMPLETE_COMPLETED : AUTOCOMPLETE_PARTIAL;
		}
		else {
			if (req_msg) print_message(req_msg);
			int result = request_strings(&rl, 0);
			if (result != ERROR) {
				result = result >= 0 ? result : -result - 2;
				/* Delete EXTERNAL_FLAG_CHAR at the end of the strings if necessary. */
				if (rl.entries[result][strlen(rl.entries[result]) - 1] == EXTERNAL_FLAG_CHAR) rl.entries[result][strlen(rl.entries[result]) - 1] = 0;
				p = str_dup(rl.entries[result]);
				*error = AUTOCOMPLETE_COMPLETED;
			}
			else *error = AUTOCOMPLETE_CANCELLED;
			reset_window();
		}
	}
	else *error = AUTOCOMPLETE_NO_MATCH;

	req_list_free(&rl);
	D(fprintf(stderr, "autocomp returning '%s', entries: %d\n", p, rl.cur_entries);)
	return p;