Exemplo n.º 1
0
/* Substitute variable name by its value and restore to wordlist */
wordlist *
cp_variablesubst(wordlist *wlist)
{
    wordlist *wl;

    for (wl = wlist; wl; wl = wl->wl_next) {

        char *s_dollar;
        int i = 0;

        while ((s_dollar = strchr(wl->wl_word + i, cp_dol)) != NULL) {

            int prefix_len = (int) (s_dollar - wl->wl_word);

            char *tail = span_var_expr(s_dollar + 1);
            char *var = copy_substring(s_dollar + 1, tail);

            wordlist *nwl = vareval(var);
            tfree(var);

            if (nwl) {
                char *x = nwl->wl_word;
                char *tail_ = copy(tail);
                nwl->wl_word = tprintf("%.*s%s", prefix_len, wl->wl_word, nwl->wl_word);
                free(x);
                if (wlist == wl)
                    wlist = nwl;
                wl = wl_splice(wl, nwl);
                i = (int) strlen(wl->wl_word);
                x = wl->wl_word;
                wl->wl_word = tprintf("%s%s", wl->wl_word, tail_);
                free(x);
                free(tail_);
            } else if (prefix_len || *tail) {
                char *x = wl->wl_word;
                wl->wl_word = tprintf("%.*s%s", prefix_len, wl->wl_word, tail);
                i = prefix_len;
                free(x);
            } else {
                wordlist *next = wl->wl_next;
                if (wlist == wl)
                    wlist = next;
                wl_delete_slice(wl, next);
                if (!next)
                    return wlist;
                wl = next;
                i = 0;
            }
        }
    }

    return (wlist);
}
Exemplo n.º 2
0
/* Attempt to process an SMB file sharing URI, as described in the
 * January 8, 2007 IETF draft titled "SMB File Sharing URI Scheme",
 * as submitted by Christopher R. Hertel and the Samba Team.
 *
 * This implementation does not use the parameters which the draft
 * describes since they are not supported by the Amiga smbfs program.
 *
 * This function will return NULL in case of failure. Processing and
 * validating the individual URI components is the job of the caller,
 * we only try to parse the URI string here. Note that the parser is
 * not particularly sophisticated...
 */
struct smb_url_args *
parse_smb_url_args(const char * arg)
{
	size_t len = strlen(arg);

	size_t domain_start = 0;
	size_t domain_end = 0;
	size_t username_start = 0;
	size_t username_end = 0;
	size_t password_start = 0;
	size_t password_end = 0;
	size_t server_start = 0;
	size_t server_end = 0;
	size_t port_start = 0;
	size_t port_end = 0;
	size_t share_start = 0;
	size_t share_end = 0;
	size_t path_start = 0;
	size_t path_end = 0;
	size_t i;

	struct smb_url_args * result = NULL;
	struct smb_url_args * smb_url_args = NULL;

	ENTER();

	/* This should be an SMB url to begin with. */
	if(len <= 6 || !could_be_smb_url(arg))
	{
		SHOWMSG("not a valid SMB url");
		goto out;
	}

	/* Skip the "smb://" part. */
	arg += 6;
	len -= 6;

	smb_url_args = allocate_smb_url_args();
	if(smb_url_args == NULL)
		goto out;

	/* Try to find the optional domain name, user name
	 * and password in the URL. We look for the '@' character
	 * which separates this optional part from the
	 * server name.
	 */
	for(i = 0 ; i < len ; i++)
	{
		if(arg[i] == '@')
		{
			size_t at = i;
			size_t j;

			/* Could there be a domain name in front
			 * of the user name?
			 */
			for(j = 0 ; j < at ; j++)
			{
				if(arg[j] == ';')
				{
					domain_end = j;

					username_start = j+1;
					break;
				}
			}

			/* Try to obtain the user name and the
			 * optional password.
			 */
			for(j = username_start ; j <= at ; j++)
			{
				if(j == at || arg[j] == ':')
				{
					username_end = j;

					/* The password follows the ':'
					 * character, if there is one.
					 */
					if(j < at)
					{
						password_start = j+1;
						password_end = at;
					}

					break;
				}
			}

			/* The server name should follow the
			 * '@' character.
			 */
			server_start = at+1;

			break;
		}
	}

	/* Try to find the server name, which may be followed
	 * by a port number/service name, the share name
	 * or the parameter list.
	 */
	for(i = server_start ; i <= len ; i++)
	{
		if(i == len || arg[i] == '/' || arg[i] == ':')
		{
			server_end = i;

			if(i < len)
			{
				/* The port number/service name follow the
				 * ':' character.
				 */
				if(arg[i] == ':')
				{
					size_t j;

					port_start = i+1;

					/* Figure out how long the port number/service
					 * name text is, and pick up the start of the
					 * share name or the parameter list.
					 */
					for(j = port_start ; j <= len ; j++)
					{
						if(j == len || arg[j] == '/' || arg[j] == '?')
						{
							port_end = j;

							/* Did we find the share name? */
							if(j < len && arg[j] == '/')
								share_start = j+1;

							break;
						}
					}
				}
				/* We'll look for the share name instead.
				 * Of course, we could look for the parameter
				 * list, but the SMB URI is none too useful
				 * without the share name, so we prefer that
				 * instead.
				 */
				else
				{
					share_start = i+1;
				}
			}

			break;
		}
	}

	/* Try to find the share name, and pick up the
	 * path name or the parameter list which may
	 * follow it.
	 */
	if(share_start > 0)
	{
		for(i = share_start ; i <= len ; i++)
		{
			if(i == len || arg[i] == '/' || arg[i] == '?')
			{
				share_end = i;

				if(i < len)
				{
					/* Pick up the path name? */
					if(arg[i] == '/')
						path_start = i+1;
				}

				break;
			}
		}
	}

	/* Try to pick up the path name. */
	if(path_start > 0)
	{
		for(i = path_start ; i <= len ; i++)
		{
			if(i == len || arg[i] == '?')
			{
				path_end = i;
				break;
			}
		}
	}

	if(domain_start < domain_end)
	{
		smb_url_args->domain = copy_substring(arg, domain_start, domain_end);
		if(smb_url_args->domain == NULL)
		{
			SHOWMSG("not enough memory");
			goto out;
		}

		replace_escape_sequences(smb_url_args->domain, domain_end - domain_start);

		D(("domain: '%s'", smb_url_args->domain));
	}
	else
	{
		SHOWMSG("no domain name provided");
	}

	if(username_start < username_end)
	{
		smb_url_args->username = copy_substring(arg, username_start, username_end);
		if(smb_url_args->username == NULL)
		{
			SHOWMSG("not enough memory");
			goto out;
		}

		replace_escape_sequences(smb_url_args->username, username_end - username_start);

		D(("username: '******'", smb_url_args->username));
	}
	else
	{
		SHOWMSG("no user name provided");
	}

	if(password_start < password_end)
	{
		smb_url_args->password = copy_substring(arg, password_start, password_end);
		if(smb_url_args->password == NULL)
		{
			SHOWMSG("not enough memory");
			goto out;
		}

		replace_escape_sequences(smb_url_args->password, password_end - password_start);

		D(("password: ..."));
	}
	else
	{
		SHOWMSG("no password provided");
	}

	if(server_start < server_end)
	{
		smb_url_args->server = copy_substring(arg, server_start, server_end);
		if(smb_url_args->server == NULL)
		{
			SHOWMSG("not enough memory");
			goto out;
		}

		replace_escape_sequences(smb_url_args->server, server_end - server_start);

		D(("server: '%s'", smb_url_args->server));
	}
	else
	{
		SHOWMSG("no server name provided");
	}

	if(port_start < port_end)
	{
		smb_url_args->port = copy_substring(arg, port_start, port_end);
		if(smb_url_args->port == NULL)
		{
			SHOWMSG("not enough memory");
			goto out;
		}

		replace_escape_sequences(smb_url_args->port, port_end - port_start);

		D(("port: '%s'", smb_url_args->port));
	}
	else
	{
		SHOWMSG("no port number/service name provided");
	}

	if(share_start < share_end)
	{
		smb_url_args->share = copy_substring(arg, share_start, share_end);
		if(smb_url_args->share == NULL)
		{
			SHOWMSG("not enough memory");
			goto out;
		}

		replace_escape_sequences(smb_url_args->share, share_end - share_start);

		D(("share: '%s'", smb_url_args->share));
	}
	else
	{
		SHOWMSG("no share name provided");
	}

	if(path_start < path_end)
	{
		smb_url_args->path = copy_substring(arg, path_start, path_end);
		if(smb_url_args->path == NULL)
		{
			SHOWMSG("not enough memory");
			goto out;
		}

		replace_escape_sequences(smb_url_args->path, path_end - path_start);

		D(("path: '%s'", smb_url_args->path));
	}
	else
	{
		SHOWMSG("no path name provided");
	}

	result = smb_url_args;
	smb_url_args = NULL;

 out:

	if(smb_url_args != NULL)
		free_smb_url_args(smb_url_args);

	RETURN(result);
	return(result);
}
Exemplo n.º 3
0
Arquivo: parse.c Projeto: imr/ngspice
int
PPlex(YYSTYPE *lvalp, struct PPltype *llocp, char **line)
{
    static char *specials = " \t%()-^+*,/|&<>~=";
    char  *sbuf = *line;
    int token;

    while ((*sbuf == ' ') || (*sbuf == '\t'))
        sbuf++;

    llocp->start = sbuf;

#define lexer_return(token_, length)                            \
    do { token = token_; sbuf += length; goto done; } while(0)

    if ((sbuf[0] == 'g') && (sbuf[1] == 't') &&
        strchr(specials, sbuf[2])) {
        lexer_return('>', 2);
    } else if ((sbuf[0] == 'l') && (sbuf[1] == 't') &&
               strchr(specials, sbuf[2])) {
        lexer_return('<', 2);
    } else if ((sbuf[0] == 'g') && (sbuf[1] == 'e') &&
               strchr(specials, sbuf[2])) {
        lexer_return(TOK_GE, 2);
    } else if ((sbuf[0] == 'l') && (sbuf[1] == 'e') &&
               strchr(specials, sbuf[2])) {
        lexer_return(TOK_LE, 2);
    } else if ((sbuf[0] == 'n') && (sbuf[1] == 'e') &&
               strchr(specials, sbuf[2])) {
        lexer_return(TOK_NE, 2);
    } else if ((sbuf[0] == 'e') && (sbuf[1] == 'q') &&
               strchr(specials, sbuf[2])) {
        lexer_return('=', 2);
    } else if ((sbuf[0] == 'o') && (sbuf[1] == 'r') &&
               strchr(specials, sbuf[2])) {
        lexer_return('|', 2);
    } else if ((sbuf[0] == 'a') && (sbuf[1] == 'n') &&
               (sbuf[2] == 'd') && strchr(specials, sbuf[3])) {
        lexer_return('&', 3);
    } else if ((sbuf[0] == 'n') && (sbuf[1] == 'o') &&
               (sbuf[2] == 't') && strchr(specials, sbuf[3])) {
        lexer_return('~', 3);
    }

    switch (*sbuf) {

    case '[':
    case ']':
        lexer_return(*sbuf, 1);

    case '>':
    case '<':
    {
        /* Workaround, The Frontend makes "<>" into "< >" */
        int j = 1;
        while (isspace_c(sbuf[j]))
            j++;
        if (((sbuf[j] == '<') || (sbuf[j] == '>')) && (sbuf[0] != sbuf[j])) {
            /* Allow both <> and >< for NE. */
            lexer_return(TOK_NE, j+1);
        } else if (sbuf[1] == '=') {
            lexer_return((sbuf[0] == '>') ? TOK_GE : TOK_LE, 2);
        } else {
            lexer_return(*sbuf, 1);
        }
    }

    case '?':
    case ':':
    case ',':
    case '+':
    case '-':
    case '*':
    case '%':
    case '/':
    case '^':
    case '(':
    case ')':
    case '=':
    case '&':
    case '|':
    case '~':
        lexer_return(*sbuf, 1);

    case '\0':
        lexer_return(*sbuf, 0);

    case '"':
    {
        char *start = ++sbuf;
        while (*sbuf && (*sbuf != '"'))
            sbuf++;
        lvalp->str = copy_substring(start, sbuf);
        if (*sbuf)
            sbuf++;
        lexer_return(TOK_STR, 0);
    }

    default:
    {
        char *s = sbuf;
        double *td = ft_numparse(&s, FALSE);
        if ((!s || *s != ':') && td) {
            sbuf = s;
            lvalp->num = *td;
            lexer_return(TOK_NUM, 0);
        } else {
            int atsign = 0;
            char *start = sbuf;
            /* It is bad how we have to recognise '[' -- sometimes
             * it is part of a word, when it defines a parameter
             * name, and otherwise it isn't.
             *
             * what is valid here ?
             *   foo  dc1.foo  dc1.@m1[vth]
             * this too ?
             *   vthing#branch
             * should we convert the pseudo identifier ?
             *   i(v5) --> v5#branch
             */
            for (; *sbuf && !strchr(specials, *sbuf); sbuf++)
                if (*sbuf == '@')
                    atsign = 1;
                else if (!atsign && *sbuf == '[')
                    break;
                else if (*sbuf == ']') {
                    if (atsign)
                        sbuf++;
                    break;
                }

            lvalp->str = copy_substring(start, sbuf);
            lexer_return(TOK_STR, 0);
        }
    }
    }

done:
    if (ft_parsedb) {
        if (token == TOK_STR)
            fprintf(stderr, "lexer: TOK_STR, \"%s\"\n", lvalp->str);
        else if (token == TOK_NUM)
            fprintf(stderr, "lexer: TOK_NUM, %G\n", lvalp->num);
        else
            fprintf(stderr, "lexer: token %d\n", token);
    }

    *line = sbuf;
    llocp->stop = sbuf;
    return (token);
}