void parse_packet(size_t query_length, unsigned char *packet) { const char *hostname; unsigned char *responsep = packet + query_length; DNS_header *response_header = malloc(sizeof(DNS_header)); memcpy(response_header, packet, sizeof(DNS_header)); int resource_records = ntohs(response_header->ancount); printf("resource records returned: %d\n", resource_records); int record_count = 0; while(record_count < resource_records && *responsep) { if(record_count) { printf("\n"); } // check for pointer if (*responsep == 0xc0) { uint16_t offset = ntohs(*(uint16_t *)responsep) & 0x3fff ; /* remove pointer bits */ hostname = parse_label(packet, offset); } else { hostname = parse_label(packet, query_length); } printf("%-10s %s\n", ">> FQDN:", hostname); responsep += strlen((const char *)responsep); uint16_t type = ntohs(*(uint16_t *)responsep); printf("%-10s %s\n", ">> TYPE:", address_types[type - 1]); responsep += 2; uint16_t class = ntohs(*(uint16_t *)responsep); printf("%-10s %s\n", ">> TYPE:", address_classes[class - 1]); responsep += 2; uint32_t ttl = ntohs(*(uint32_t *)responsep); printf("%-10s %u\n", ">> TTL:", ttl); responsep += 4; uint16_t rdlength = ntohs(*(uint16_t *)responsep); printf("%-10s %hu\n", ">> RDLENGTH:", rdlength); responsep += 2; printf("%-10s", ">> RDATA"); for(int i = 0; i < rdlength; i++) { if(i) { putchar('.'); } printf("%d", *responsep); responsep++; } putchar('\n'); record_count++; free((void *)hostname); } free(response_header); }
struct grecs_node * grecs_node_from_path_locus(const char *path, const char *value, grecs_locus_t *plocus, grecs_locus_t *vallocus) { int rc; int i; int argc; char **argv; struct grecs_node *dn = NULL; rc = split_cfg_path(path, &argc, &argv, NULL); if (rc) return NULL; dn = grecs_node_create(grecs_node_stmt, NULL); dn->ident = argv[argc - 1]; if (value) { struct grecs_value *gval = parse_label(value); if (vallocus) gval->locus = *vallocus; dn->v.value = gval; } else dn->v.value = NULL; for (i = argc - 2; i >= 0; i--) { struct grecs_value *label = NULL; struct grecs_node *node; char *p, *q = argv[i]; do { p = strchr(q, '='); if (p && p > argv[i] && p[-1] != '\\') { *p++ = 0; label = parse_label(p); break; } else if (p) q = p + 1; else break; } while (*q); node = grecs_node_create(grecs_node_block, plocus); node->ident = argv[i]; if (label) node->v.value = label; node->down = dn; if (dn) dn->up = node; dn = node; } if (plocus) fixup_loci(dn, plocus, vallocus ? &vallocus->end : NULL); free(argv); return dn; }
boolean parse(FILE *file, char* name) { char line_buf[LINE_MAX_CHARS]; char *line = line_buf; char *part; boolean success = TRUE; file_name = name; while (!feof(file)) { fgets(line, LINE_MAX_CHARS, file); line_counter++; /* check for label */ part = strchr(line, ':'); if (part != NULL) { if (part == line) { *part = 0; } else { part = strtok(line, ":"); } success = parse_label(part); line = strtok(NULL, ""); } if (success) { line = trim(line); if (*line == '.') { success = call_parse_func(directives_table, "directive", line); } else { success = call_parse_func(commands_table, "command", line); } } } return success; }
void item::parse_vw_input(char *line) { parse_label(line); // strcpy(expanded_line_, line); // substring stmp = {expanded_line_, expanded_line_ + strlen(line)}; substring stmp = {line, line + strlen(line)}; // after first bar follows the quality features char * first_barloc = safe_index(stmp.begin, '|', stmp.end); // after second bar follows the similiary features char * second_barloc = safe_index(first_barloc + 1, '|', stmp.end); // construct vec_feat for quality features construct_feat_vec(first_barloc+1, second_barloc-1, vec_quality_feat_); // construct vec_feat for user-specified similiarty features, but they will not be used for regression/ranking construct_feat_vec(second_barloc+1, stmp.end, vec_sim_feat_); /* int numcopychars = second_barloc - line; // strncpy( expanded_line_, line, numcopychars); // expanded_line_[numcopychars] = '\0'; //have to manually put end strncpy( prefix_line_, line, numcopychars); prefix_line_[numcopychars] = '\0'; //have to manually put end */ int numcopychars = second_barloc - first_barloc; strncpy( prefix_line_, first_barloc, numcopychars); prefix_line_[numcopychars] = '\0'; //have to manually put end }
static int split_cfg_path(const char *path, int *pargc, char ***pargv, grecs_value_t ***pvalv) { int argc; char **argv; char *delim = "."; char static_delim[2] = { 0, 0 }; if (path[0] == '\\') { argv = calloc(2, sizeof (*argv)); if (!argv) return WRDSE_NOSPACE; argv[0] = strdup(path + 1); if (!argv[0]) { free(argv); return WRDSE_NOSPACE; } argv[1] = NULL; argc = 1; } else { int rc; struct wordsplit ws; if (strchr("./:;,^~", path[0])) { delim = static_delim; delim[0] = path[0]; path++; } ws.ws_delim = delim; rc = wordsplit(path, &ws, WRDSF_DELIM | WRDSF_DEFFLAGS); if (rc) return rc; argc = ws.ws_wordc; argv = ws.ws_wordv; ws.ws_wordc = 0; ws.ws_wordv = NULL; wordsplit_free(&ws); } *pargv = argv; *pargc = argc; if (pvalv) { int i; grecs_value_t **valv; valv = grecs_calloc(argc, sizeof(valv[0])); for (i = 0; i < argc; i++) { char *p = strchr(argv[i], '='); if (p) { *p++ = 0; valv[i] = parse_label(p); } } *pvalv = valv; } return 0; }
int parse_block(int fd1,int fd2) { char tag; read(fd1,&tag,1); switch(GET_TAG(tag)) { case 0x00: parse_titleblock(fd1); break; case 0x01: parse_sheet(fd1); break; case 0x02: parse_component(fd1,fd2); break; case 0x03: parse_wire(fd1); break; case 0x04: parse_bus(fd1); break; case 0x05: parse_junction(fd1); break; case 0x06: parse_port(fd1); break; case 0x07: parse_label(fd1); break; case 0x08: parse_entry(fd1); break; case 0x09: parse_dashed(fd1); break; case 0x0a: parse_power(fd1); break; case 0x0b: parse_text(fd1); break; case 0x0c: parse_marker(fd1); break; case 0x0f: return 0; break; default: fprintf(stderr,"\nUnknown Block Tag\n"); exit(-1); break; } return 1; }
static void edit_label(struct sun_disklabel *sl, const char *disk, const char *bootpath) { char tmpfil[] = _PATH_TMPFILE; const char *editor; int status; FILE *fp; pid_t pid; pid_t r; int fd; int c; if ((fd = mkstemp(tmpfil)) < 0) err(1, "mkstemp"); if ((fp = fdopen(fd, "w")) == NULL) err(1, "fdopen"); print_label(sl, disk, fp); fflush(fp); for (;;) { if ((pid = fork()) < 0) err(1, "fork"); if (pid == 0) { if ((editor = getenv("EDITOR")) == NULL) editor = _PATH_VI; execlp(editor, editor, tmpfil, (char *)NULL); err(1, "execlp %s", editor); } status = 0; while ((r = wait(&status)) > 0 && r != pid) ; if (WIFEXITED(status)) { if (parse_label(sl, tmpfil) == 0) { fclose(fp); unlink(tmpfil); write_label(sl, disk, bootpath); return; } printf("re-edit the label? [y]: "); fflush(stdout); c = getchar(); if (c != EOF && c != '\n') while (getchar() != '\n') ; if (c == 'n') { fclose(fp); unlink(tmpfil); return; } } } fclose(fp); unlink(tmpfil); return; }
static void filter_client_line(void) { int offset; const char* cmd; if (!handle_auth_response(&linebuf, 0)) { if ((offset = parse_label()) > 0) { cmd = linebuf.s + offset; /* If we see a "AUTHENTICATE" or "LOGIN" command, save the preceding * label for reference when looking for the corresponding "OK" */ if(!strncasecmp(cmd, "AUTHENTICATE ", 13)) { if (handle_auth_parameter(&linebuf, offset + 13)) str_copy(&saved_label, &label); } else if (!strncasecmp(cmd, "LOGIN ", 6)) handle_login(offset + 6); } } }
static boolean translate( struct translate_ctx *ctx ) { eat_opt_white( &ctx->cur ); if (!parse_header( ctx )) return FALSE; if (ctx->processor == TGSI_PROCESSOR_TESS_CTRL || ctx->processor == TGSI_PROCESSOR_TESS_EVAL) ctx->implied_array_size = 32; while (*ctx->cur != '\0') { uint label_val = 0; if (!eat_white( &ctx->cur )) { report_error( ctx, "Syntax error" ); return FALSE; } if (*ctx->cur == '\0') break; if (parse_label( ctx, &label_val )) { if (!parse_instruction( ctx, TRUE )) return FALSE; } else if (str_match_nocase_whole( &ctx->cur, "DCL" )) { if (!parse_declaration( ctx )) return FALSE; } else if (str_match_nocase_whole( &ctx->cur, "IMM" )) { if (!parse_immediate( ctx )) return FALSE; } else if (str_match_nocase_whole( &ctx->cur, "PROPERTY" )) { if (!parse_property( ctx )) return FALSE; } else if (!parse_instruction( ctx, FALSE )) { return FALSE; } } return TRUE; }
int parse_instruction(int fd_src, t_parser *parser, t_labels **list) { char *line; char **my_tab; line = get_next_instruction(fd_src, parser); while (line != NULL) { my_tab = my_str_to_wordtab(line, SEPARATOR_CHAR); if (parse_label(parser, my_tab, list) == 1) { free_tab(my_tab); free(line); return (1); } free_tab(my_tab); free(line); line = get_next_instruction(fd_src, parser); } return (0); }
static boolean translate( struct translate_ctx *ctx ) { eat_opt_white( &ctx->cur ); if (!parse_header( ctx )) return FALSE; while (*ctx->cur != '\0') { uint label_val = 0; if (!eat_white( &ctx->cur )) { report_error( ctx, "Syntax error" ); return FALSE; } if (*ctx->cur == '\0') break; if (parse_label( ctx, &label_val )) { if (!parse_instruction( ctx, TRUE )) return FALSE; } else if (str_match_no_case( &ctx->cur, "DCL" )) { if (!parse_declaration( ctx )) return FALSE; } else if (str_match_no_case( &ctx->cur, "IMM" )) { if (!parse_immediate( ctx )) return FALSE; } else if (str_match_no_case( &ctx->cur, "PROPERTY" )) { if (!parse_property( ctx )) return FALSE; } else if (!parse_instruction( ctx, FALSE )) { return FALSE; } } return TRUE; }
/* should check range on field size using len */ WORD * parse_immed(char *immed, int len) { static WORD value; char **endptr=NULL; WORD *temp; if( !immed ) return NULL; /* empty string */ if( !strlen(immed) ) return NULL; /* also empty */ /* check to see if label */ if( (temp=parse_label(immed)) ) { value=*temp; } else { /* not label */ value=strtol(immed, endptr, 0); /* machine dependent - this sucks */ if(endptr) return NULL; /* bad format in immediate */ } /* now value is correct, check for range */ return &value; }
int parse_listing() { int result = 0; if ((token = yylex()) == ID) { char* language = strduplicate(string); char* highlighting; char* caption; if ((token = yylex()) == LST_FRAME) { /* highlight rows in frame */ highlighting = parse_highlighting(); token = yylex(); } else highlighting = strduplicate(""); switch (token) { case LST_SEPARATOR: caption = strduplicate(parse_label()); result = parse_listing_text(language,caption,highlighting); free(caption); break; case NEWLINE: result = parse_listing_text(language,NULL,highlighting); break; default: fprintf(stderr, "[%d] Listing mode: Separator '-' expected, found %s \"%s\" instead\n", yylineno, get_token_name(token), yytext); result = 0; break; } free(highlighting); free(language); } else { fprintf(stderr, "[%d] Listing mode: Language name expected, but %s \"%s\"\n", yylineno, get_token_name(token), yytext); result = 0; } return result; }
static void filter_server_data(char* data, ssize_t size) { if(saved_label.len > 0) { /* Skip continuation data */ if(data[0] != '+') { int resp; /* Check if the response is tagged with the saved label */ str_copyb(&linebuf, data, size); resp = parse_label(); if(resp > 0) { if(!str_diff(&label, &saved_label)) { log_line(data, size); /* Check if the response was an OK */ if(!strncasecmp(linebuf.s + resp, "OK ", 3)) accept_client(username.s); else deny_client(username.s); str_truncate(&saved_label, 0); } } } } write_client(data, size); }
int main(int ac, char **av) { char *file; struct btrfs_root *root; struct btrfs_trans_handle *trans; char *label = NULL; char *first_file; u64 block_count = 0; u64 dev_block_count = 0; u64 blocks[7]; u64 alloc_start = 0; u64 metadata_profile = 0; u64 data_profile = 0; u32 leafsize = sysconf(_SC_PAGESIZE); u32 sectorsize = 4096; u32 nodesize = leafsize; u32 stripesize = 4096; int zero_end = 1; int option_index = 0; int fd; int ret; int i; int mixed = 0; int data_profile_opt = 0; int metadata_profile_opt = 0; int discard = 1; int ssd = 0; int force_overwrite = 0; char *source_dir = NULL; int source_dir_set = 0; u64 num_of_meta_chunks = 0; u64 size_of_data = 0; u64 source_dir_size = 0; int dev_cnt = 0; int saved_optind; char estr[100]; u64 features = 0; while(1) { int c; c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:VMK", long_options, &option_index); if (c < 0) break; switch(c) { case 'A': alloc_start = parse_size(optarg); break; case 'f': force_overwrite = 1; break; case 'd': data_profile = parse_profile(optarg); data_profile_opt = 1; break; case 'l': case 'n': nodesize = parse_size(optarg); leafsize = parse_size(optarg); break; case 'L': label = parse_label(optarg); break; case 'm': metadata_profile = parse_profile(optarg); metadata_profile_opt = 1; break; case 'M': mixed = 1; break; case 'O': { char *orig = strdup(optarg); char *tmp = orig; tmp = parse_fs_features(tmp, &features); if (tmp) { fprintf(stderr, "Unrecognized filesystem feature '%s'\n", tmp); free(orig); exit(1); } free(orig); if (features & BTRFS_FEATURE_LIST_ALL) { list_all_fs_features(); exit(0); } break; } case 's': sectorsize = parse_size(optarg); break; case 'b': block_count = parse_size(optarg); if (block_count <= 1024*1024*1024) { printf("SMALL VOLUME: forcing mixed " "metadata/data groups\n"); mixed = 1; } zero_end = 0; break; case 'V': print_version(); break; case 'r': source_dir = optarg; source_dir_set = 1; break; case 'K': discard = 0; break; default: print_usage(); } } sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE)); if (check_leaf_or_node_size(leafsize, sectorsize)) exit(1); if (check_leaf_or_node_size(nodesize, sectorsize)) exit(1); saved_optind = optind; dev_cnt = ac - optind; if (dev_cnt == 0) print_usage(); if (source_dir_set && dev_cnt > 1) { fprintf(stderr, "The -r option is limited to a single device\n"); exit(1); } while (dev_cnt-- > 0) { file = av[optind++]; if (is_block_device(file)) if (test_dev_for_mkfs(file, force_overwrite, estr)) { fprintf(stderr, "Error: %s", estr); exit(1); } } optind = saved_optind; dev_cnt = ac - optind; file = av[optind++]; ssd = is_ssd(file); if (is_vol_small(file)) { printf("SMALL VOLUME: forcing mixed metadata/data groups\n"); mixed = 1; if (metadata_profile != data_profile) { if (metadata_profile_opt || data_profile_opt) { fprintf(stderr, "With mixed block groups data and metadata profiles must be the same\n"); exit(1); } } } /* * Set default profiles according to number of added devices. * For mixed groups defaults are single/single. */ if (!mixed) { if (!metadata_profile_opt) { if (dev_cnt == 1 && ssd) printf("Detected a SSD, turning off metadata " "duplication. Mkfs with -m dup if you want to " "force metadata duplication.\n"); metadata_profile = (dev_cnt > 1) ? BTRFS_BLOCK_GROUP_RAID1 : (ssd) ? 0: BTRFS_BLOCK_GROUP_DUP; } if (!data_profile_opt) { data_profile = (dev_cnt > 1) ? BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */ } } else { metadata_profile = 0; data_profile = 0; } ret = test_num_disk_vs_raid(metadata_profile, data_profile, dev_cnt, mixed, estr); if (ret) { fprintf(stderr, "Error: %s\n", estr); exit(1); } /* if we are here that means all devs are good to btrfsify */ printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION); printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n"); dev_cnt--; if (!source_dir_set) { /* * open without O_EXCL so that the problem should not * occur by the following processing. * (btrfs_register_one_device() fails if O_EXCL is on) */ fd = open(file, O_RDWR); if (fd < 0) { fprintf(stderr, "unable to open %s: %s\n", file, strerror(errno)); exit(1); } first_file = file; ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count, block_count, &mixed, discard); if (block_count && block_count > dev_block_count) { fprintf(stderr, "%s is smaller than requested size\n", file); exit(1); } } else { fd = open_target(file); if (fd < 0) { fprintf(stderr, "unable to open the %s\n", file); exit(1); } first_file = file; source_dir_size = size_sourcedir(source_dir, sectorsize, &num_of_meta_chunks, &size_of_data); if(block_count < source_dir_size) block_count = source_dir_size; ret = zero_output_file(fd, block_count, sectorsize); if (ret) { fprintf(stderr, "unable to zero the output file\n"); exit(1); } /* our "device" is the new image file */ dev_block_count = block_count; } /* To create the first block group and chunk 0 in make_btrfs */ if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) { fprintf(stderr, "device is too small to make filesystem\n"); exit(1); } blocks[0] = BTRFS_SUPER_INFO_OFFSET; for (i = 1; i < 7; i++) { blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 + leafsize * i; } /* * FS features that can be set by other means than -O * just set the bit here */ if (mixed) features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS; if ((data_profile | metadata_profile) & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) { features |= BTRFS_FEATURE_INCOMPAT_RAID56; } process_fs_features(features); ret = make_btrfs(fd, file, label, blocks, dev_block_count, nodesize, leafsize, sectorsize, stripesize, features); if (ret) { fprintf(stderr, "error during mkfs: %s\n", strerror(-ret)); exit(1); } root = open_ctree(file, 0, OPEN_CTREE_WRITES); if (!root) { fprintf(stderr, "Open ctree failed\n"); close(fd); exit(1); } root->fs_info->alloc_start = alloc_start; ret = make_root_dir(root, mixed); if (ret) { fprintf(stderr, "failed to setup the root directory\n"); exit(1); } trans = btrfs_start_transaction(root, 1); if (dev_cnt == 0) goto raid_groups; btrfs_register_one_device(file); zero_end = 1; while (dev_cnt-- > 0) { int old_mixed = mixed; file = av[optind++]; /* * open without O_EXCL so that the problem should not * occur by the following processing. * (btrfs_register_one_device() fails if O_EXCL is on) */ fd = open(file, O_RDWR); if (fd < 0) { fprintf(stderr, "unable to open %s: %s\n", file, strerror(errno)); exit(1); } ret = btrfs_device_already_in_root(root, fd, BTRFS_SUPER_INFO_OFFSET); if (ret) { fprintf(stderr, "skipping duplicate device %s in FS\n", file); close(fd); continue; } ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count, block_count, &mixed, discard); mixed = old_mixed; BUG_ON(ret); ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count, sectorsize, sectorsize, sectorsize); BUG_ON(ret); btrfs_register_one_device(file); } raid_groups: if (!source_dir_set) { ret = create_raid_groups(trans, root, data_profile, data_profile_opt, metadata_profile, metadata_profile_opt, mixed, ssd); BUG_ON(ret); } ret = create_data_reloc_tree(trans, root); BUG_ON(ret); printf("fs created label %s on %s\n\tnodesize %u leafsize %u " "sectorsize %u size %s\n", label, first_file, nodesize, leafsize, sectorsize, pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy))); printf("%s\n", BTRFS_BUILD_VERSION); btrfs_commit_transaction(trans, root); if (source_dir_set) { trans = btrfs_start_transaction(root, 1); ret = create_chunks(trans, root, num_of_meta_chunks, size_of_data); BUG_ON(ret); btrfs_commit_transaction(trans, root); ret = make_image(source_dir, root, fd); BUG_ON(ret); } ret = close_ctree(root); BUG_ON(ret); free(label); return 0; }
int main (int argc, char **argv) { stack_init(&stack, "Stack"); stack_init(&callstack, "Call stack"); stack_init(&auxstack, "Auxiliary stack"); srand(time(NULL)); if (argc == 2) { if (!strcmp(argv[1], "--about")){ printf("PURPLE (PURPoseless LanguagE) Interpreter v. 0.1\n"); printf("Compiled on %s at %s\n", __DATE__, __TIME__); exit(0); } else { fp = fopen(argv[1], "r+"); if (fp == NULL) { fprintf(stderr, "error opening file at: %s\n", argv[1]); quit(); } } } else { printf("Usage: %s [--about | <file-name>]\n", argv[0]); exit(1); } int line = 1; int startspecial = 0; readLine = malloc(1024*sizeof(char)); while (fgets(readLine, 1024, fp)) { readLine = str_trim(readLine); if (readLine[0] == ':') { for (i=0; readLine[i]; i++) readLine[i] = tolower(readLine[i]); readLine[i] = '\0'; labels[labelnum].location = ftell(fp); labels[labelnum].name = malloc(sizeof(char)*(strlen(readLine))); strncpy(labels[labelnum].name, readLine+1, strlen(readLine)); labels[labelnum].name = str_trim(labels[labelnum].name); if (!strcmp(labels[labelnum].name, "start")) startspecial = 1; labelnum++; } if (readLine[0] == '~') { for (i=0; readLine[i]; i++) readLine[i] = tolower(readLine[i]); readLine[i] = '\0'; functions[functionnum].location = ftell(fp); functions[functionnum].name = malloc(sizeof(char)*(strlen(readLine))); strcpy(functions[functionnum].name, readLine+1); functions[functionnum].name = str_trim(functions[functionnum].name); functionnum++; } line++; } fseek(fp, 0, SEEK_SET); line = 1; if (startspecial) parse_label("start", 0); while (fgets(readLine, 1000, fp)) { parse_line(readLine, line); line++; } printf("\nReached end without finding `END`!\n"); free(readLine); quit(); }
void parse_line (char *line, int linenum) { char *command, *temp, numstring[32]; int arguments[64]; int stringmode = 0, numindex = 0, numtopush = 0, argindex = -1; line = str_trim(line); if(str_prefix(line, "#")) return; command = malloc(strlen(line)*sizeof(char)+1); for (i = 0; !isspace(line[i]) && line[i] != '\0'; i++) command[i] = line[i]; command[i] = '\0'; for (i=0; command[i]; i++) command[i] = tolower(command[i]); if(str_prefix(command, ":") || str_prefix(command, "~")) { free(command); return; } temp = str_dup(&line[strlen(command)]); temp = str_trim(temp); if(!strcmp(command, "goto")) { parse_label(temp, linenum); free(temp); free(command); return; } if(!strcmp(command, "zbr")) { if(!stack_peek(&stack)) { parse_label(temp, linenum); } free(temp); free(command); return; } if(!strcmp(command, "nzbr")) { if(stack_peek(&stack)) { parse_label(temp, linenum); } free(temp); free(command); return; } if(!strcmp(command, "ebr")) { if (stack_empty(&stack)) { parse_label(temp, linenum); } free(temp); free(command); return; } if(!strcmp(command, "nebr")) { if (!stack_empty(&stack)) { parse_label(temp, linenum); } free(temp); free(command); return; } if(strcmp(temp, "")) temp = strcat(temp, ","); for (i = 0; temp[i]; i++) { if (temp[i] == '"') { stringmode = !stringmode; } else if (stringmode) { if (temp[i] != '\0') { argindex++; arguments[argindex] = temp[i]; } } else { /* number mode */ if (temp[i] == ',') { if (numindex != 0) { //i.e. it's not "" /* if numstring IS "", most likely it's a comma after a string entry, which we don't want to push 0 for */ numstring[numindex] = '\0'; if (str_parseint(numstring, &numtopush)) { argindex++; arguments[argindex] = numtopush; } else { fprintf(stderr, "\ninvalid integer: `%s` at line %d", numstring, linenum); } numindex = 0; } } else if (isdigit(temp[i]) || temp[i] == '-') { numstring[numindex] = temp[i]; numindex++; } else if (!isspace(temp[i])) { fprintf(stderr, "\nunexpected character: `%c` at line %d", temp[i], linenum); numindex = 0; } } } if (stringmode) { fprintf(stderr, "\nunclosed quote at line %d", linenum); free(temp); free(command); return; } else { numstring[numindex] = '\0'; if (str_parseint(numstring, &numtopush)) { argindex++; arguments[argindex] = numtopush; } else { fprintf(stderr, "\ninvalid integer: `%s` at line %d", numstring, linenum); } } /* now push the array onto the stack, in reverse order */ for (i = argindex-1; i >= 0; i--) { stack_push(&stack, arguments[i]); } if (argindex > 0) { argcount = argindex; } /* printf("\nline %d: parsing command `%s`.", linenum, command); */ parse_command(command, linenum); free(command); free(temp); }
/* * primop = ( conexpr ) * | integer * | label */ int parse_primop(char **p) { char lbl[MAX_SYMLEN]; char *pp; long val; int idx; skip_ws(p); pp = *p; /* handle parenthesized expressions */ if (*pp == '(') { pp++; /* skip open paren */ val = parse_logop(&pp); skip_ws(&pp); if (*pp != ')') { error("missing closing paren"); } *p = pp+1; /* skip closing paren. */ return val; } /* handle integers and labels */ if (*pp == '0') { pp++; if (*pp == 'x') { /* hex number: 0x[0-9a-f]+ */ pp++; if (!ishexdigit(*pp)) { /* must have at least one digit after the "0x" */ error("badly formed hex number"); *p = pp; return 0; } val = hexval(*pp); pp++; while (ishexdigit(*pp)) { val = 16*val + hexval(*pp); pp++; } } else { /* octal number: 0[0-7]* */ val = 0; while (isoctdigit(*pp)) { val = 8*val + (*pp) - '0'; pp++; } } } else if (isdigit((unsigned)*pp)) { /* presumably decimal value */ val = 0; while (isdigit((unsigned)*pp)) { val = 10*val + (*pp) - '0'; pp++; } } else { /* assume it is a label */ parse_label(p, lbl); if (strlen(lbl) == 0) { /* oops, what is it? */ error("error: expected constant expression"); return 0; } idx = find_sym_idx(lbl); if (idx) { val = symtab[idx].value; } else { val = 0x00; /* dummy value */ if (g_pass == 2) error("couldn't find symbol in second pass"); } pp = *p; /* nullifies later assignment */ } *p = pp; return val; }
/* * Disk label editor for sun disklabels. */ int main(int ac, char **av) { struct sun_disklabel sl; const char *bootpath; const char *proto; const char *disk; int ch; bootpath = _PATH_BOOT; while ((ch = getopt(ac, av, "b:BcehnrRw")) != -1) switch (ch) { case 'b': bflag = 1; bootpath = optarg; break; case 'B': Bflag = 1; break; case 'c': cflag = 1; break; case 'e': eflag = 1; break; case 'h': hflag = 1; break; case 'n': nflag = 1; break; case 'r': fprintf(stderr, "Obsolete -r flag ignored\n"); break; case 'R': Rflag = 1; break; case 'w': wflag = 1; break; default: usage(); break; } if (bflag && !Bflag) usage(); if (nflag && !(Bflag || eflag || Rflag || wflag)) usage(); if (eflag && (Rflag || wflag)) usage(); if (eflag) hflag = 0; ac -= optind; av += optind; if (ac == 0) usage(); bzero(&sl, sizeof(sl)); disk = av[0]; if (wflag) { if (ac != 2 || strcmp(av[1], "auto") != 0) usage(); read_label(&sl, disk); bzero(sl.sl_part, sizeof(sl.sl_part)); sl.sl_part[SUN_RAWPART].sdkp_cyloffset = 0; sl.sl_part[SUN_RAWPART].sdkp_nsectors = sl.sl_ncylinders * sl.sl_ntracks * sl.sl_nsectors; write_label(&sl, disk, bootpath); } else if (eflag) { if (ac != 1) usage(); read_label(&sl, disk); if (sl.sl_magic != SUN_DKMAGIC) errx(1, "%s%s has no sun disklabel", _PATH_DEV, disk); edit_label(&sl, disk, bootpath); } else if (Rflag) { if (ac != 2) usage(); proto = av[1]; read_label(&sl, disk); if (parse_label(&sl, proto) != 0) errx(1, "%s: invalid label", proto); write_label(&sl, disk, bootpath); } else if (Bflag) { read_label(&sl, disk); if (sl.sl_magic != SUN_DKMAGIC) errx(1, "%s%s has no sun disklabel", _PATH_DEV, disk); write_label(&sl, disk, bootpath); } else { read_label(&sl, disk); if (sl.sl_magic != SUN_DKMAGIC) errx(1, "%s%s has no sun disklabel", _PATH_DEV, disk); print_label(&sl, disk, stdout); } return (0); }