Exemplo n.º 1
0
Arquivo: prompt.c Projeto: gonzus/tig
enum request
open_prompt(struct view *view)
{
	char *cmd = read_prompt(":");
	const char *argv[SIZEOF_ARG] = { NULL };
	int argc = 0;

	if (cmd && !argv_from_string(argv, &argc, cmd)) {
		report("Too many arguments");
		return REQ_NONE;
	}

	return run_prompt_command(view, argv);
}
Exemplo n.º 2
0
Arquivo: argv.c Projeto: apieum/tig
static bool
format_expand_arg(struct format_context *format, const char *name, const char *end)
{
	struct format_var *vars = format->vars;
	int i;

	if (!prefixcmp(name, "%(prompt")) {
		const char *prompt = "Command argument: ";
		char msgbuf[SIZEOF_STR];
		const char *value;
		const char *msgstart = name + STRING_SIZE("%(prompt");
		int msglen = end - msgstart - 1;

		if (end && msglen > 0 && string_format(msgbuf, "%.*s", msglen, msgstart)) {
			const char *msg = msgbuf;

			while (isspace(*msg))
				msg++;
			if (*msg)
				prompt = msg;
		}

		value = read_prompt(prompt);
		if (value == NULL)
			return FALSE;
		return string_format_from(format->buf, &format->bufpos, "%s", value);
	}

	for (i = 0; i < format->vars_size; i++) {
		const char *value;

		if (strncmp(name, vars[i].name, vars[i].namelen))
			continue;

		if (vars[i].value == argv_env.file && !format->file_filter)
			return TRUE;

		value = *vars[i].value ? vars[i].value : vars[i].value_if_empty;
		if (!*value)
			return TRUE;

		return string_format_from(format->buf, &format->bufpos, "%s", value);
	}

	return FALSE;
}
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
    int ret = 0;
    int isPrivate = 0;

    if (parseArgs(argc, argv)) {
        printf("Syntax: %s --input-file=infile --output-file=outfile [--decrypt] [--password=pwd] [--salt=salt] "
               "[--vector-multiplier=number] [--type=base64|binary] [--simple-mode] [--key-size <keysize> "
               "--key-file <keyfile-prefix>] [--dump-vectors <dump-file>]\n",
               argv[0]);
        return 1;
    }

    if (use_minimal == 1) {
        int c = 0;
        char *in, *key, *salt;

        printf("User requested minimal encryption/decryption. Overriding ...\n");
        printf("Use encryption or decryption (E/D) ? ");
        while ((c != 'e') && (c != 'E') && (c != 'd') && (c != 'D')) {
            c = getchar();
        }

        while (getchar() != '\n') ;

        printf("%scryption selected\n", ((c == 'D') || (c == 'd')) ? "De" : "En");

        in = read_prompt("Enter input data: ");
        key = read_prompt("Enter encryption key: ");
        salt = read_prompt("Enter encryption salt: ");

        if ((c == 'D') || (c == 'd'))
            printf("Output: %s\n", mincrypt_decrypt_minimal(in, key, salt));
        else
            printf("Output: %s\n", mincrypt_encrypt_minimal(in, key, salt));
        return 0;
    }

    if (salt == NULL)
        salt = DEFAULT_SALT_VAL;

    if (password == NULL) {
        char *tmp;

        tmp = Wgetpass("Enter password value: ");
        if (tmp == NULL) {
            printf("Error: No password entered\n");
            return 1;
        }
        password = strdup(tmp);
        free(tmp);
    }

    /* Use base 4 numbering system for password and salt encoding */
    if (use_four_bs == 1) {
        unsigned char *tmp1 = NULL;

        tmp1 = mincrypt_convert_to_four_system((unsigned char *)salt, strlen(salt));
        salt = strdup(tmp1);
        free(tmp1);

        tmp1 = mincrypt_convert_to_four_system((unsigned char *)password, strlen(password));
        password = strdup(tmp1);
        free(tmp1);
    }

    if (keysize > 0) {
        int ret;
        char public_key[4096] = { 0 };
        char private_key[4096] = { 0 };

        snprintf(private_key, sizeof(private_key), "%s.key", keyfile);
        snprintf(public_key, sizeof(public_key), "%s.pub", keyfile);

        printf("Generating keys based on input data. This may take a while...\n");
        ret = mincrypt_generate_keys(keysize, salt, password, private_key, public_key);
        printf("Key generation done. Keys saved as { private = '%s', public = '%s' }\n",
               private_key, public_key);
        return ret;
    }

    if (keyfile != NULL) {
        int ret;

        if ((ret = mincrypt_read_key_file(keyfile, &isPrivate)) != 0) {
            fprintf(stderr, "Error while reading key file '%s' (error code %d, %s)\n", keyfile, ret, strerror(-ret));
            return 2;
        }

        DPRINTF("Key file %s contains %s key\n", keyfile, isPrivate ? "private" : "public");

        if (isPrivate && !decrypt) {
            fprintf(stderr, "Error: Cannot use private key for encryption\n");
            return 3;
        }

        if (!isPrivate && decrypt) {
            fprintf(stderr, "Error: Cannot use public key for decryption\n");
            return 3;
        }
    }

    if ((type != NULL) && (strcmp(type, "base64") == 0))
        if (mincrypt_set_encoding_type(ENCODING_TYPE_BASE64) != 0)
            printf("Warning: Cannot set base64 encoding, using binary encoding instead\n");

    if (simple_mode)
        if (mincrypt_set_simple_mode(1) != 0)
            printf("Warning: Cannot set simple mode for non-binary encoding\n");

    if (!decrypt)
        ret = mincrypt_encrypt_file(infile, outfile, password, salt, vector_mult);
    else
        ret = mincrypt_decrypt_file(infile, outfile, password, salt, vector_mult);

    if (dump_file != NULL)
        mincrypt_dump_vectors(dump_file);

    mincrypt_cleanup();

    if (ret != 0)
        fprintf(stderr, "Action failed with error code: %d\n", ret);
    else
        printf("Action has been completed successfully\n");

    return ret;
}
Exemplo n.º 4
0
/*
 * cscope_add --
 *	The cscope add command.
 */
static int
cscope_add(SCR *sp, EXCMD *cmdp, const CHAR_T *dname)
{
	struct stat sb;
	EX_PRIVATE *exp;
	CSC *csc;
	size_t len;
	int cur_argc;
	const char *dbname;
	char path[MAXPATHLEN];
	const char *np;
	char *npp;
	size_t nlen;

	exp = EXP(sp);

	/*
	 *  0 additional args: usage.
	 *  1 additional args: matched a file.
	 * >1 additional args: object, too many args.
	 */
	cur_argc = cmdp->argc;
	if (argv_exp2(sp, cmdp, dname, STRLEN(dname))) {
		return (1);
	}
	if (cmdp->argc == cur_argc) {
		(void)csc_help(sp, "add");
		return (1);
	}
	if (cmdp->argc == cur_argc + 1)
		dname = cmdp->argv[cur_argc]->bp;
	else {
		INT2CHAR(sp, dname, STRLEN(dname)+1, np, nlen);
		ex_emsg(sp, np, EXM_FILECOUNT);
		return (1);
	}

	INT2CHAR(sp, dname, STRLEN(dname)+1, np, nlen);

	/*
	 * The user can specify a specific file (so they can have multiple
	 * Cscope databases in a single directory) or a directory.  If the
	 * file doesn't exist, we're done.  If it's a directory, append the
	 * standard database file name and try again.  Store the directory
	 * name regardless so that we can use it as a base for searches.
	 */
	if (stat(np, &sb)) {
		msgq(sp, M_SYSERR, "%s", np);
		return (1);
	}
	if (S_ISDIR(sb.st_mode)) {
		(void)snprintf(path, sizeof(path),
		    "%s/%s", np, CSCOPE_DBFILE);
		if (stat(path, &sb)) {
			msgq(sp, M_SYSERR, "%s", path);
			return (1);
		}
		dbname = CSCOPE_DBFILE;
	} else if ((npp = strrchr(np, '/')) != NULL) {
		*npp = '\0';
		dbname = npp + 1;
	} else {
		dbname = np;
		np = ".";
	}

	/* Allocate a cscope connection structure and initialize its fields. */
	len = strlen(np);
	CALLOC_RET(sp, csc, CSC *, 1, sizeof(CSC) + len);
	csc->dname = csc->buf;
	csc->dlen = len;
	memcpy(csc->dname, np, len);
	csc->mtime = sb.st_mtime;

	/* Get the search paths for the cscope. */
	if (get_paths(sp, csc))
		goto err;

	/* Start the cscope process. */
	if (run_cscope(sp, csc, dbname))
		goto err;

	/*
	 * Add the cscope connection to the screen's list.  From now on, 
	 * on error, we have to call terminate, which expects the csc to
	 * be on the chain.
	 */
	LIST_INSERT_HEAD(&exp->cscq, csc, q);

	/* Read the initial prompt from the cscope to make sure it's okay. */
	return read_prompt(sp, csc);

err:	free(csc);
	return (1);
}
Exemplo n.º 5
0
int main()
{
	SaHpiSessionIdT sid;
	SaHpiEventT event;
	SaHpiRptEntryT rpt_entry;
	SaHpiResourceIdT rid;
	SaHpiSeverityT severity_old, severity_new;
	int i;
	SaErrorT status;
	int ret = SAF_TEST_UNKNOWN;

	printf("\n*****************Domain func begin***************\n");

	status = saHpiSessionOpen(SAHPI_UNSPECIFIED_DOMAIN_ID, &sid, NULL);

	if (status != SA_OK) {
		e_print(saHpiSessionOpen, SA_OK, status);
		ret = SAF_TEST_UNRESOLVED;
	} else {

		status = saHpiSubscribe(sid);

		if (status != SA_OK) {
			e_print(saHpiSubscribe, SA_OK, status);
			ret = SAF_TEST_UNRESOLVED;
		} else {
			/* User inserts a FRU manually */
			read_prompt("Now pls. insert a FRU in the domain ...\n"
				    "Then press Enter key to continue ...\n");

			for (i = 0;;) {
				status =
				    saHpiEventGet(sid, TIMEOUT, &event, NULL,
						  &rpt_entry, NULL);

				if (status == SA_ERR_HPI_TIMEOUT
				    && ++i == TIMES) {
					m_print
					    ("The timeout value has been reached, but the user did not enter an FRU.");
					ret = SAF_TEST_UNRESOLVED;
					break;
				} else if (status != SA_OK) {
					e_print(saHpiEventGet, SA_OK, status);
					ret = SAF_TEST_UNRESOLVED;
					break;
				} else if ((event.EventType == SAHPI_ET_HOTSWAP) &&
					       ((event.EventDataUnion.HotSwapEvent.HotSwapState == SAHPI_HS_STATE_ACTIVE) ||
					        (event.EventDataUnion.HotSwapEvent.HotSwapState == SAHPI_HS_STATE_INSERTION_PENDING))) {
					rid = rpt_entry.ResourceId;
					severity_old = rpt_entry.ResourceSeverity;
					break;
				}
			}

			if (ret == SAF_TEST_UNKNOWN) {

				if (severity_old != SAHPI_CRITICAL)
					severity_new = SAHPI_CRITICAL;
				else
					severity_new = SAHPI_MAJOR;

				status = saHpiResourceSeveritySet(sid, rid, severity_new);

				if (status != SA_OK) {
					e_print(saHpiResourceSeveritySet, SA_OK, status);
					ret = SAF_TEST_UNRESOLVED;
				} else {

					read_prompt
					    ("Now please create a \"surprise extraction\" by removing the\n"
						 "the same FRU you just inserted to identify its ResourceId ...\n"
					     "Then press the Enter key to continue ...\n");

					for (i = 0;;) {
						status = saHpiEventGet(sid, TIMEOUT, &event, NULL,
								               &rpt_entry, NULL);

						if (status == SA_ERR_HPI_TIMEOUT) {
							if (++i == TIMES) {
								m_print
							   	 ("The timeout value has been reached, but the user did not plug out an FRU.");
								ret = SAF_TEST_UNRESOLVED;
								break;
							}
						} else if (status != SA_OK) {
							e_print(saHpiEventGet, SA_OK, status);
							ret = SAF_TEST_UNRESOLVED;
							break;
						} else if (event.EventType == SAHPI_ET_HOTSWAP && 
								   event.EventDataUnion.HotSwapEvent.HotSwapState == SAHPI_HS_STATE_NOT_PRESENT &&
								   rpt_entry.ResourceId == rid && event.Severity == severity_new) {
							ret = SAF_TEST_PASS;
							break;
						}
					}
				}

				if (ret == SAF_TEST_UNKNOWN) {
					ret = SAF_TEST_FAIL;
				}

				saHpiResourceSeveritySet(sid, rid, severity_old);
			}

			status = saHpiUnsubscribe(sid);
		}

		status = saHpiSessionClose(sid);
	}

	printf("\n  return=%s\n",get_test_result(ret));
	printf("\n*****************Domain func end*****************\n");

	return ret;
}