Lines Matching defs:pc
71 PathCache *pc; /* The path-list cache in which to look up the executables */
126 char *dir; /* The directory pathname (stored in pc->sg) */
131 /* and its contents are taken from pc->abs_mem */
132 /* or pc->rel_mem */
138 static int add_PathNode(PathCache *pc, const char *dirname);
166 void *data; /* Annonymous data to be passed to pc->check_fn() */
174 static void pca_clear_cache(PathCache *pc);
177 * Read a username from string[] and record it in pc->usrnam[].
179 static int pca_read_username(PathCache *pc, const char *string, int slen,
186 static int pca_extract_dir(PathCache *pc, const char *path,
193 static int pca_scan_dir(PathCache *pc, const char *dirname, CacheMem *mem);
213 static int pca_init_PcaPathConf(PcaPathConf *ppc, PathCache *pc);
219 static int pca_prepare_suffix(PathCache *pc, const char *suffix,
230 static const char *pca_prepare_prefix(PathCache *pc, const char *prefix,
235 * place the corresponding home directory into pc->path. Otherwise
236 * just clear pc->path.
238 static int pca_expand_tilde(PathCache *pc, const char *path, int pathlen,
245 static void pca_remove_marks(PathCache *pc);
269 PathCache *pc; /* The object to be returned */
273 pc = (PathCache *)malloc(sizeof(PathCache));
274 if(!pc) {
283 pc->err = NULL;
284 pc->node_mem = NULL;
285 pc->abs_mem = NULL;
286 pc->rel_mem = NULL;
287 pc->head = NULL;
288 pc->tail = NULL;
289 pc->path = NULL;
290 pc->home = NULL;
291 pc->dr = NULL;
292 pc->cfc = NULL;
293 pc->check_fn = 0;
294 pc->data = NULL;
295 pc->usrnam[0] = '\0';
299 pc->err = _new_ErrMsg();
300 if(!pc->err)
301 return del_PathCache(pc);
305 pc->node_mem = _new_FreeList(sizeof(PathNode), PATH_NODE_BLK);
306 if(!pc->node_mem)
307 return del_PathCache(pc);
311 pc->abs_mem = new_CacheMem();
312 if(!pc->abs_mem)
313 return del_PathCache(pc);
317 pc->rel_mem = new_CacheMem();
318 if(!pc->rel_mem)
319 return del_PathCache(pc);
323 pc->path = _new_PathName();
324 if(!pc->path)
325 return del_PathCache(pc);
329 pc->home = _new_HomeDir();
330 if(!pc->home)
331 return del_PathCache(pc);
335 pc->dr = _new_DirReader();
336 if(!pc->dr)
337 return del_PathCache(pc);
341 pc->cfc = new_CplFileConf();
342 if(!pc->cfc)
343 return del_PathCache(pc);
348 cfc_set_check_fn(pc->cfc, pc->check_fn, pc->data);
352 return pc;
360 * pc PathCache * The cache to be deleted (can be NULL).
364 PathCache *del_PathCache(PathCache *pc)
366 if(pc) {
370 pc->err = _del_ErrMsg(pc->err);
374 pc->node_mem = _del_FreeList(pc->node_mem, 1);
378 pc->abs_mem = del_CacheMem(pc->abs_mem);
379 pc->rel_mem = del_CacheMem(pc->rel_mem);
384 pc->head = NULL;
385 pc->tail = NULL;
389 pc->path = _del_PathName(pc->path);
393 pc->home = _del_HomeDir(pc->home);
397 pc->dr = _del_DirReader(pc->dr);
401 pc->cfc = del_CplFileConf(pc->cfc);
405 free(pc);
420 * pc PathCache * The filename cache.
432 void pca_set_check_fn(PathCache *pc, CplCheckFn *check_fn, void *data)
434 if(pc) {
440 if(check_fn != pc->check_fn || data != pc->data)
441 pca_remove_marks(pc);
445 pc->check_fn = check_fn;
446 pc->data = data;
451 cfc_set_check_fn(pc->cfc, check_fn, data);
460 * pc PathCache * The filename cache that suffered the error.
464 const char *pca_last_error(PathCache *pc)
466 return pc ? _err_get_msg(pc->err) : "NULL PathCache argument";
473 * pc PathCache * The cache to be cleared.
475 static void pca_clear_cache(PathCache *pc)
477 if(pc) {
481 _rst_FreeList(pc->node_mem);
482 pc->head = pc->tail = NULL;
486 rst_CacheMem(pc->abs_mem);
487 rst_CacheMem(pc->rel_mem);
497 * pc PathCache * The cache in which to store the names of
508 * pca_last_error(pc).
510 int pca_scan_path(PathCache *pc, const char *path)
514 char **fptr; /* A pointer into pc->abs_mem->files[] */
518 if(!pc)
523 pca_clear_cache(pc);
532 * on the fly into pc->pathname, then add them to the list of path
539 * Extract the next pathname component into pc->path->name.
541 if(pca_extract_dir(pc, pptr, &pptr))
548 if(add_PathNode(pc, pc->path->name))
553 * pc->abs_mem->files[]. Record pointers to the starts of each
555 * be done in add_PathNode(), because pc->abs_mem->files[] may
559 fptr = pc->abs_mem->files;
560 for(node=pc->head; node; node=node->next) {
572 * pc PathCache * The cache of filenames.
579 * return int 0 - OK. The extracted path is in pc->path->name.
581 * have been left in pc->err.
583 static int pca_extract_dir(PathCache *pc, const char *path, const char **nextp)
590 * place the corresponding home directory into pc->path. Otherwise
591 * just clear pc->path.
593 if(pca_expand_tilde(pc, path, strlen(path), 0, &pptr))
609 if(_pn_append_to_path(pc->path, sptr, pptr - sptr, 1) == NULL) {
610 _err_record_msg(pc->err, "Insufficient memory to record directory name",
620 int dirlen = strlen(pc->path->name);
622 strncmp(pc->path->name + dirlen - FS_DIR_SEP_LEN, FS_DIR_SEP,
624 if(_pn_append_to_path(pc->path, FS_DIR_SEP, FS_DIR_SEP_LEN, 0) == NULL) {
625 _err_record_msg(pc->err, "Insufficient memory to record directory name",
649 * pc PathCache * The cache of filenames.
658 * return int 0 - OK. The username can be found in pc->usrnam.
660 * can be found in pc->err.
662 static int pca_read_username(PathCache *pc, const char *string, int slen,
665 int usrlen; /* The number of characters in pc->usrnam[] */
686 pc->usrnam[usrlen++] = *sptr;
693 _err_record_msg(pc->err, "Username too long", END_ERR_MSG);
699 pc->usrnam[usrlen] = '\0';
802 * pc PathCache * The filename cache.
808 static int add_PathNode(PathCache *pc, const char *dirname)
825 node = (PathNode *) _new_FreeListNode(pc->node_mem);
827 _err_record_msg(pc->err, "Insufficient memory to cache new directory.",
836 node->mem = relative ? pc->rel_mem : pc->abs_mem;
843 node->dir = _sg_store_string(pc->abs_mem->sg, dirname, 0);
845 _err_record_msg(pc->err, "Insufficient memory to store directory name.",
855 int nfile = node->nfile = pca_scan_dir(pc, node->dir, node->mem);
857 node = (PathNode *) _del_FreeListNode(pc->node_mem, node);
864 if(pc->head) {
865 pc->tail->next = node;
866 pc->tail = node;
868 pc->head = pc->tail = node;
878 * pc PathCache * The filename cache.
888 static int pca_scan_dir(PathCache *pc, const char *dirname, CacheMem *mem)
896 if(_dr_open_dir(pc->dr, dirname, NULL))
901 while((filename = _dr_next_file(pc->dr))) {
906 _pn_clear_path(pc->path);
907 if(_pn_append_to_path(pc->path, " ", 1, 0) == NULL ||
908 _pn_append_to_path(pc->path, filename, -1, 1) == NULL) {
909 _err_record_msg(pc->err, "Insufficient memory to record filename",
916 copy = _sg_store_string(mem->sg, pc->path->name, 0);
918 _err_record_msg(pc->err, "Insufficient memory to cache file name.",
933 _err_record_msg(pc->err,
991 * pc PathCache * The cached list of files.
1008 * pointer points to memory owned by *pc, and
1012 char *pca_lookup_file(PathCache *pc, const char *name, int name_len,
1020 if(!pc || !name || name_len==0)
1038 if(pca_expand_tilde(pc, name, name_len, literal, &nptr) ||
1039 _pn_append_to_path(pc->path, nptr, name_len - (nptr-name),
1042 return pc->path->name;
1049 for(node=pc->head; node; node=node->next) {
1056 if(pca_scan_dir(pc, node->dir, node->mem) < 1)
1065 _pn_clear_path(pc->path);
1066 if(_pn_append_to_path(pc->path, name, name_len, !literal) == NULL)
1071 match = (char **)bsearch(pc->path->name, node->files, node->nfile,
1078 if(_pn_prepend_to_path(pc->path, node->dir, -1, 0) == NULL)
1083 if(!pc->check_fn || (*match)[0] == PCA_F_WANTED ||
1084 ((*match)[0]==PCA_F_ENIGMA && pc->check_fn(pc->data, pc->path->name))){
1086 return pc->path->name;
1128 * pc PathCache * The filename cache in which to look for
1133 * can be found by calling pca_last_error(pc).
1135 PcaPathConf *new_PcaPathConf(PathCache *pc)
1141 if(!pc)
1148 _err_record_msg(pc->err, "Insufficient memory.", END_ERR_MSG);
1156 if(pca_init_PcaPathConf(ppc, pc))
1166 * pc PathCache * The cache in which completions will be looked up.
1170 * obtained by calling pca_last_error(pc).
1172 static int pca_init_PcaPathConf(PcaPathConf *ppc, PathCache *pc)
1177 if(!pc)
1183 ppc->pc = pc;
1200 ppc->pc = NULL; /* It is up to the caller to delete the cache */
1218 PathCache *pc; /* The cache in which to look for completions */
1253 pc = ppc->pc;
1282 cfc_file_start(pc->cfc, word_start);
1283 return cpl_file_completions(cpl, pc->cfc, line, word_end);
1290 for(node=pc->head; node; node=node->next) {
1297 if(pca_scan_dir(pc, node->dir, node->mem) < 1)
1306 * pc->path.
1308 prefix = pca_prepare_prefix(pc, start_path, prefix_len, ppc->escaped);
1352 _pn_clear_path(pc->path);
1353 if(_pn_append_to_path(pc->path, node->dir, -1, 0) == NULL ||
1354 _pn_append_to_path(pc->path, match+1, -1, 0) == NULL) {
1355 _err_record_msg(pc->err, "Insufficient memory to complete file name",
1362 if(!pc->check_fn || match[0] == PCA_F_WANTED ||
1363 (match[0]==PCA_F_ENIGMA && pc->check_fn(pc->data, pc->path->name))) {
1366 * Copy the completion suffix into the work pathname pc->path->name,
1369 if(pca_prepare_suffix(pc, match + 1 + prefix_len,
1375 if(cpl_add_completion(cpl, line, word_start, word_end, pc->path->name,
1392 prefix = pca_prepare_prefix(pc, start_path, prefix_len, ppc->escaped);
1398 if(_dr_open_dir(pc->dr, FS_PWD, NULL))
1404 while((filename = _dr_next_file(pc->dr))) {
1412 if(pca_prepare_suffix(pc, filename + prefix_len, ppc->escaped) ||
1413 cpl_add_completion(cpl, line, word_start, word_end, pc->path->name,
1417 * The prefix in pc->path->name will have been overwritten by
1420 prefix = pca_prepare_prefix(pc, start_path, prefix_len, ppc->escaped);
1425 _dr_close_dir(pc->dr);
1430 * Using the work buffer pc->path, make a suitably escaped copy of a
1434 * pc PathCache * The filename cache resource object.
1441 static int pca_prepare_suffix(PathCache *pc, const char *suffix,
1454 _pn_clear_path(pc->path);
1473 if(_pn_resize_path(pc->path, suffix_len + nbsl) == NULL) {
1474 _err_record_msg(pc->err, "Insufficient memory to complete file name",
1483 strlcpy(pc->path->name, suffix, pc->path->dim);
1490 char *dst = pc->path->name;
1547 * If needed make a new copy of the prefix being matched, in pc->path->name,
1552 * pc PathCache * The cache being searched.
1559 * left in pc->err.
1561 static const char *pca_prepare_prefix(PathCache *pc, const char *prefix,
1568 _pn_clear_path(pc->path);
1569 if(_pn_append_to_path(pc->path, prefix, prefix_len, 1) == NULL) {
1570 _err_record_msg(pc->err, "Insufficient memory to complete filename",
1574 return pc->path->name;
1619 * either an empty string in pc->path if there is no ~user expression,
1623 * pc PathCache * The filename cache.
1634 * in pc->err).
1636 static int pca_expand_tilde(PathCache *pc, const char *path, int pathlen,
1644 _pn_clear_path(pc->path);
1652 * it, into pc->usrnam[].
1654 if(pca_read_username(pc, ++pptr, pathlen-1, literal, &pptr))
1659 homedir = _hd_lookup_home_dir(pc->home, pc->usrnam);
1661 _err_record_msg(pc->err, _hd_last_home_dir_error(pc->home), END_ERR_MSG);
1667 if(_pn_append_to_path(pc->path, homedir, -1, 0) == NULL) {
1668 _err_record_msg(pc->err,
1698 * pc PathCache * The filename cache.
1700 static void pca_remove_marks(PathCache *pc)
1708 for(node=pc->head; node; node=node->next) {