void import_keep(uint8_t do_inst, const char *import_file) { int list_size = 0; char **pkglist = NULL, *match; char input[BUFSIZ], fullpkgname[BUFSIZ], query[BUFSIZ]; FILE *fp; if ((fp = fopen(import_file, "r")) == NULL) err(EXIT_FAILURE, MSG_ERR_OPEN, import_file); while (fgets(input, BUFSIZ, fp) != NULL) { if (!isalnum((int)input[0])) continue; trimcr(input); if (strchr(input, '/') != NULL) { snprintf(query, BUFSIZ, GET_PKGNAME_BY_PKGPATH, input); if ((pkgindb_doquery(query, pdb_get_value, fullpkgname)) == PDB_OK) XSTRDUP(match, fullpkgname); else match = NULL; } else match = unique_pkg(input, REMOTE_PKG); if (match == NULL) { fprintf(stderr, MSG_PKG_NOT_AVAIL, input); continue; } /* 1st element + NULL */ XREALLOC(pkglist, (list_size + 2) * sizeof(char *)); pkglist[list_size] = match; pkglist[++list_size] = NULL; } fclose(fp); if (pkglist == NULL) errx(EXIT_FAILURE, MSG_EMPTY_IMPORT_LIST); pkgin_install(pkglist, do_inst); free_list(pkglist); }
char * read_repos() { FILE *fp; char *tmp, *b, *repos = NULL, buf[BUFSIZ]; int curlen = 0, repolen = 0; const struct VarParam *vp; if ((fp = fopen(PKGIN_CONF"/"REPOS_FILE, "r")) == NULL) return NULL; while (!feof(fp)) { memset(buf, 0, BUFSIZ); (void)fgets(buf, BUFSIZ, fp); if (strncmp(buf, "ftp://", 6) != 0 && strncmp(buf, "http://", 7) != 0 && strncmp(buf, "https://", 8) != 0 && strncmp(buf, "file://", 7) != 0) continue; /* * Try to replace all the ocurrences with the proper * system values. */ for (vp = var; vp->func != NULL; vp++) { if (strstr(buf, vp->opt) != NULL) { tmp = vp->func(); if (tmp == NULL) { warn(MSG_TRANS_FAILED, vp->opt); continue; } if ((b = strreplace(buf, vp->opt, tmp)) != NULL) strncpy(buf, b, BUFSIZ); else warn(MSG_INVALID_REPOS, buf); XFREE(tmp); XFREE(b); } } if (trimcr(buf) < 0) /* strip newline */ continue; curlen = strlen(buf) + 2; /* ' ' + '\0' */ repolen += curlen; XREALLOC(repos, repolen * sizeof(char)); if (repolen > curlen) /* more than one repo */ { /* add a space character */ curlen = strlen(repos); repos[curlen] = ' '; repos[curlen + 1] = '\0'; } else /* 1st entry */ memset(repos, 0, curlen); strcat(repos, buf); } fclose(fp); return repos; }
/* * expect "str" (a regular expression) on file descriptor "fd", storing * the FTP return code of the command in the integer "ftprc". The "str" * string is expected to match some FTP return codes after a '\n', e.g. * "\n(550|226).*\n" */ static int expect(int fd, const char *str, int *ftprc) { int rc; char *p, buf[90], progress_str[90]; char tmp[MAXLEN]; #if EXPECT_DEBUG char *vstr; #endif /* EXPECT_DEBUG */ regex_t rstr; int done; struct pollfd set[1]; int retval; regmatch_t match; int verbose_expect=0; static int next = 1; #if EXPECT_DEBUG vstr=malloc(2*sizeof(buf)); if (vstr == NULL) err(EXIT_FAILURE, "expect: malloc() failed"); strvis(vstr, str, VIS_NL|VIS_SAFE|VIS_CSTYLE); #endif /* EXPECT_DEBUG */ if (regcomp(&rstr, str, REG_EXTENDED) != 0) err(EXIT_FAILURE, "expect: regcomp() failed"); #if EXPECT_DEBUG if (expect_debug) printf("expecting \"%s\" on fd %d ...\n", vstr, fd); #endif /* EXPECT_DEBUG */ if(0) setbuf(stdout, NULL); memset(buf, '\n', sizeof(buf)); done=0; retval=0; set[0].fd = fd; set[0].events = POLLIN; while(!done) { rc = poll(set, 1, 10*60*1000); /* seconds until next message from tar */ switch (rc) { case -1: if (errno == EINTR) break; warn("expect: poll() failed (probably ftp died because of bad args)"); done = 1; retval = -1; break; case 0: warnx("expect: poll() timeout"); /* need to send ftp coprocess SIGINT to make it stop * downloading into dir that we'll blow away in a second */ kill(ftp_pid, SIGINT); /* Wait until ftp coprocess is responsive again * XXX Entering recursion here! */ rc = ftp_cmd("cd .\n", "\n(550|250).*\n"); if (rc != 250) { /* now we have a really good reason to bail out ;) */ } /* ftp is at command prompt again, and will wait for our * next command. If we were downloading, we can now safely * continue and remove the dir that the tar command was * expanding to */ done = 1; /* hope that's ok */ retval = -1; break; default: if (set[0].revents & POLLHUP) { done = 1; retval = -1; break; } rc = read(fd, &buf[sizeof(buf) - 1], 1); if (rc <= 0) { done = 1; retval = -1; break; } if (verbose_expect) putchar(buf[sizeof(buf)-1]); #if EXPECT_DEBUG { char *v=malloc(2*sizeof(buf)); strvis(v, buf, VIS_NL|VIS_SAFE|VIS_CSTYLE); if (expect_debug) printf("expect=<%s>, buf=<%*s>\n", vstr, strlen(v), v); free(v); } #endif /* EXPECT_DEBUG */ /****************** pkg_select mess ! ******************/ if (store_expect) { int len; memset(tmp, 0, MAXLEN); XSTRCPY(tmp, buf); len = strlen(tmp); /* returned result has 2 lines, separated by a \n * we need the 2d one, pointed by strchr * returned string does not finish with \n\0, e.g. : * '\n' <repeats 78 times>, "---> TYPE A\n\001" */ if ((tmp[len - 2] == '\n') && ((p = strchr(tmp, '\n')) != NULL)) { p++; /* cleanup leading \n */ trimcr(p); /* callback */ fill_store(p); } } /* was not a store request */ /* used for progress bars with tar(1) */ if (use_tar && strncmp(buf, "pkgsrc/", 7) == 0 && (p = strchr(buf, '/')) != NULL) { /* point after pkgsrc/ */ strcpy(progress_str, p); /* /category/blah */ p++; /* category/blah */ if ((p = strchr(p, '/')) != NULL) { if (conf.shell_output) printf("%s\n", progress_str); else if (next) { char msg[MAXLEN]; p++; *p = '\0'; snprintf(msg, MAXLEN, "extracting pkgsrc%s", progress_str); next = progress_bar(pkgsrc_progress, msg, INCREMENTAL); } } /* extract category from path */ } /* strcmp pkgsrc/ (tar progress) */ if (regexec(&rstr, buf, 1, &match, 0) == 0) { #if EXPECT_DEBUG if (expect_debug) printf("Gotcha -> %s!\n", buf+match.rm_so+1); fflush(stdout); #endif /* EXPECT_DEBUG */ if (ftprc && isdigit((unsigned char)buf[match.rm_so+1])) *ftprc = atoi(buf+match.rm_so+1); done=1; retval=0; } memmove(buf, buf+1, sizeof(buf)-1); /* yes, this is non-performant */ break; } /* switch rc */ } #if EXPECT_DEBUG printf("done.\n"); if (str) free(vstr); #endif /* EXPECT_DEBUG */ return retval; }