Exemple #1
0
char const *s_state_insert(register State *sp, char const *key,
                                               char const *subst)
{
    while (*key)
    {
        register char const *cp;
                                        /* if key is in the set, switch to  */
        if ((cp = strchr(sp->d_str, *key)) != 0)         /*  that state  */
            sp = sp->d_next[cp - sp->d_str];
        else
        {                               /* if not, add a new state to the   */
            size_t last = string_length(&sp->d_set);      /*          set */

            string_addchar(&sp->d_set, (char)*key); /* add key's 1st char */
            sp->d_str = string_str(&sp->d_set); /* and set the state's ptr  */

                                                /* add a new state          */
            new_size(&sp->d_next, last + 1, last, sizeof(State *));
            sp = sp->d_next[last] = s_state_new();
        }
        key++;                          /* inspect the next key char        */
    }

                                        /* all key chars exhausted: insert  */
                                        /* replacement text                 */

    if (sp->d_replacement)              /* oops, it's already there         */
        return sp->d_replacement;       /* return the one found             */

    sp->d_replacement = new_str(subst);
    return 0;                           /* here 0 indicates SUCCESS         */
}
Exemple #2
0
LEXER_TOKEN l_handle_number(register Lexer *lp)
{
    do                                      /* at this point d_lastchar is  */
    {                                       /* a digit, so we may proceed   */
        string_addchar(&lp->d_text, lp->d_lastchar);
        l_nextchar(lp);
    }
    while (isdigit(lp->d_lastchar));

    l_unget(lp, lp->d_lastchar);
    return TOKEN_TEXT;
}
Exemple #3
0
LEXER_TOKEN l_handle_ident(register Lexer *lp)
{
    register int c;

    do                                      /* at this point d_lastchar is  */
    {                                       /* an identchar, so we proceed  */
        string_addchar(&lp->d_text, lp->d_lastchar);
        l_nextchar(lp);
    }
    while (l_isIdentChar(lp));

    c = lp->d_lastchar;                 /* used below                       */
    l_unget(lp, c);                     /* unget the char beyond the ident  */

                                        /* the close parentheses comments   */
                                        /* are to keep my emacs             */
                                        /* paren-matcher happy              */
    return c == '(' ? TOKEN_SYMBOL : TOKEN_TEXT;                    /*  )   */
}
Exemple #4
0
void args_construct(int argc, char **argv,
                    char *options,
                    LongOption const *longOption)
{
    char *cp;
    size_t nopt;
    struct option *long_option;
    size_t nlong = 0;

    if (!longOption)
        long_option = new_calloc(1, sizeof(struct option));
    else
    {
        while (longOption[nlong++].d_name)
            ;

        long_option = new_memory(nlong, sizeof(struct option));

        for (nopt = 0; nopt < nlong; nopt++)
        {
            long_option[nopt].name      = longOption[nopt].d_name;
            long_option[nopt].has_arg   = longOption[nopt].d_type;
            long_option[nopt].flag      = 0;
            long_option[nopt].val       = longOption[nopt].d_value;
        }
    }

    memset(&args, 0, sizeof(Args));
    string_construct(&args.d_option, 0);

    args.d_argv = argv;
    args.d_argc = argc;

    if ((cp = getenv("home")) || (cp = getenv("HOME")))     /* set home */
        args.d_home = new_str(cp);

    args.d_programName = basename(argv[0]);         /*  set programname */
    args.d_initial_dir = new_getcwd();              /* set initial dir. */

    args.d_ok = true;
    nopt = 0;

    while (true)
    {
        int optchar = getopt_long(args.d_argc, args.d_argv,
                                  options, long_option, NULL);

        switch (optchar)
        {
            default:
                string_addchar(&args.d_option, optchar);
                new_size(&args.d_optarg, nopt + 1, nopt, sizeof(char *));
                args.d_optarg[nopt++] = optarg ? new_str(optarg) : 0;
            // FALLING THROUGH
            case 'l':
            continue;                   /* at `while(true)' */

            case '?':
                args.d_ok = false;      /* stop processing at failure       */
            break;

            case EOF:
                args.d_optind = optind;
            break;
        }
        break;                          /* leave `while(true)'  */
    }
    free(long_option);
}
Exemple #5
0
int main(int argc, char **argv)
{
    String *cmd = string_new(0);
    String *input = NULL;

    if (argc == 1)
    {
        fprintf(stderr,
            "Usage: %s [-s] prog [arg(s)]\n"
            "      -s: use system call, not std-input\n"
            "    prog: program or system call to execute\n"
            "  arg(s): optional arguments to prog\n"
            "Input to prog is read from stdin, unless -s was specified\n"
            "   (don't make this too long, it's stored in a String first)\n"
            "\n",
            argv[0]);

        exit(1);
    }

    bool syscall = !strcmp(argv[1], "-s");
    if (syscall)
    {
        argc--;
        argv++;
    }
    else
    {
        input = string_new(0);
        char buffer[100];

        fprintf(stderr, "Reading input from stdin...\n");

        while (fgets(buffer, 100, stdin))
            string_addstr(input, buffer);

        fprintf(stderr, "Input will be:\n"
                        "`%s'\n", string_str(input));
    }

    while (*++argv)
    {
        string_addstr(cmd, *argv);
        string_addchar(cmd, ' ');
    }


    fprintf(stderr, "Command will be:\n"
                        "`%s'\n", string_str(cmd));

    message_setseverity(MSG_ALL);
    message(MSG_NOTICE, "Creating Process");

    Process process;
    process_construct(&process, "process-demo", cmd,
                                                input);

    if (syscall)
        process_system(&process);
    else
        process_fork(&process);

    String const *out = process_output(&process);

    fprintf(stderr, "Output from process: '\n"
            "%s\n"
            "'\n", string_str(out));

    process_destroy(&process);

    return 0;
}