Example #1
0
/* Takes a single line of source and returns a Statement that can be executed */
Statement * Parse(char *line) {
    /* Gross hack: Skip labels, which are identified by a ':', but account for
     * strings that have ':' in them
     */
    if (strchr(line, ':') && !strchr(line, '\'')) {
        return NULL;
    }

    Statement *ret;
    char *token;
    char *strlit = NULL; /* Used for holding a string literal */
    char *line_copy; /* line is broken with strtok, this holds a copy */
    char *tab;
    char *comma = ",";
    char *nl = "\n";
    bool onearg = false;
    char *commands[] = {
        "INT", "FLT", "STR", "FIL", "ADD", "SUB", "MUL", "DIV", "MOD", "INC",
        "DEC", "MOV", "CAT", "LEN", "FST", "LST", "GET", "OUT", "OPN", "CLS",
        "CMP", "JMP", "JEQ", "JNE", "JIG", "JIL", "JGE", "JLE", "EXT"
    };
    int temp;

    line_copy = malloc(strlen(line) + 1);
    strcpy(line_copy, line);

    while ((tab = strchr(line, '\t')) != NULL) {
        *tab = ' ';
    }

    /* Blank lines and comments */
    token = strtok(line, " ");
    if (token) {
        if (token[0] == '\n' || token[0] == ';') {
            free(line_copy);
            return NULL;
        }
    } else {
        free(line_copy);
        return NULL;
    }

    ret = NewStatement();
    ToUpper(token); /* For finding in commands[] array */

    /* Check the COMMANDS enum in types.h */
    /* Gets the index of the token in commands[] array */
    ret->command = arraystr(commands, __instruction_count, token);

    if (ret->command == -1) {
        ABORT("Error: unknown keyword: %s", token);
    }

    /* Retrieve first argument ******************/
    token = strtok(NULL, " ");
    if (!token) {
        ret->argcount = 0;
        free(line_copy);
        return ret;
    }

    /* Comma required after first argument */
    comma = strchr(token, ',');
    if (!comma) {
        onearg = true;
        nl = strchr(token, '\n');
        if (nl) {
            *nl = '\0';
        }
        nl = NULL;
    }
    else {
        /* Don't include comma in token */
        *comma = '\0';
    }

    ret->args[0] = CreateArg(token);

    /* Retrieve second argument **************/
    token = strtok(NULL, " ");
    if (!token || token[0] == '\n') {
        ret->argcount = 1;
        ret->args[1] = NULL;
        free(line_copy);
        return ret;
    }
    /* May have picked up a comment... somehow */
    if (token[0] == ';') {
        ret->argcount = 1;
        ret->args[1] = NULL;
        free(line_copy);
        return ret;
    }

    if (onearg) {
        ABORT("Missing comma after first argument");
    }

    nl = strchr(token, '\n');
    if (nl) {
        *nl = '\0';
    }

    ret->argcount = 2;

    /* string literals must be handled differently - tokens are seperated
     * by spaces, and a string may contain spaces, so the whole line
     * must be used for string literals. Since the first arg can't be a literal,
     * this is here.
     */
    if (token[0] == '\'') {
        strlit = malloc(80);
        strncpy(strlit, strstr(line_copy, token), 79);
        *(strchr(strlit + 1, '\'') + 1) = '\0';
    }
    free(line_copy);

    if (strlit) {
        ret->args[1] = CreateArg(strlit);
    } else {
        ret->args[1] = CreateArg(token);
    }
    return ret;
}
Example #2
0
int ls(char *path) {
    int result = 0;
    printf("Content-Type: text/html; charset=utf-8\n\n<html><head><title>%s</title><style type=\"text/css\">"
            " .K { color:green; }"
            " .M { color:orange; }"
            " .G { color:red; }"
            " td.size { text-align:right; }"
            "</style></head><body>%s<br>",path,path);
    DIR *d = opendir(path);
    if (d) {
        Array *dirs = array_new(free);
        Array *files = array_new(free);
        struct dirent *f ;
        while((f = readdir(d)) != NULL) {
            
            char *p,*u;
            double size=0;
            char *unit="b";
            int precision=0;
            char *size_str=NULL;
            struct STAT64 st;

            ovs_asprintf(&p,"%s/%s",path,f->d_name);
            //printf("<br>checking %s\n",p);
            //u = file_to_url(p);

            ovs_asprintf(&u,"?%s",p);

            util_stat(p,&st);

            size = st.st_size;
            if (f->d_type == 0) {
                // more nmt100 oddness - dirent type not set?
                if (S_ISREG(st.st_mode)) f->d_type = DT_REG;
                else if (S_ISDIR(st.st_mode)) f->d_type = DT_DIR;
            }
            if (size > 1024 ) { size /= 1024 ; unit="<span class=\"K\">K</span>" ; precision=1; }
            if (size > 1024 ) { size /= 1024 ; unit="<font class=\"M\">M</span>" ; }
            if (size > 1024 ) { size /= 1024 ; unit="<span class=\"G\">G</span>" ; }

            char *display = f->d_name;
            if (strcmp(f->d_name,"..") == 0) {
                // find parent folder name
                char *tmp = STRDUP(path);
                ovs_asprintf(&u,"?%s",dirname(tmp));
                FREE(tmp);
                display="up↑";  ; // UP
            }

            if (strcmp(f->d_name,".") != 0) {
                char *tmp;
                switch(f->d_type) {
                    case DT_REG:
                        if(strstr(f->d_name,"log.gz")) {
                            // Display inline link
                            ovs_asprintf(&tmp,"<tr><td><a href=\"%s.txt\">%s</a> <a href=\"%s\">*</a></td><td class=\"size\"> - %.*f%s</td></tr>",
                                    u,display,u, precision,size,unit);
                        } else {
                            //ovs_asprintf(&tmp,"<tr><td>%.1f%s</td><td><a href=\"%s\">%s</a></td></tr>",size,unit,u,f->d_name);
                            ovs_asprintf(&tmp,"<tr><td><a href=\"%s\">%s</a></td><td class=\"size\"> - %.*f%s</td></tr>",
                                    u,display, precision,size,unit);
                        }
                        array_add(files,tmp);
                        break;
                    case DT_DIR:
                        ovs_asprintf(&tmp,"<a href=\"%s\">%s</a>&nbsp;&nbsp;&nbsp;&nbsp; ",u,display);
                        array_add(dirs,tmp);
                        break;
                    default:
                        ovs_asprintf(&tmp,"<tr><td><a href=\"%s\">%s</a></td><td>%d?</td></tr>",u,f->d_name,f->d_type);
                        array_add(files,tmp);
                        break;
                }
            }
            FREE(size_str);
            FREE(u);
            FREE(p);
        }

        closedir(d);

        char *out;
        array_sort(dirs,NULL);
        out = arraystr(dirs);
        if (out) {
            printf("%s",out);
        }
        FREE(out);

        printf("<hr><table border=\"0\">");
        array_sort(files,NULL);
        out = arraystr(files);
        if (out) {
            printf("%s",out);
        }
        FREE(out);
        printf("</table>");

        array_free(dirs);
        array_free(files);
    } else {
        fprintf(stderr,"Error %d opening [%s]\n",errno,path);
        result = errno;
    }
    printf("</body></html>");
    return result;
}