📄 scheme.c
字号:
/* * Copyright (c) 2005 Alf Schlichting * All rights reserved. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */#include <ctype.h>#include <err.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include "pathnames.h"#include "common.h"#include "cpuctl.h"int line_count;extern struct scheme s;extern struct fields fld[];/* find and return a scheme from the configuration file */voidread_scheme(struct scheme *sch, char *name, char *path){ FILE *conf; char buf[BUF_SIZE]; char *p, *pp; int found_scheme; int found_scheme_name; if ((conf = fopen(path, "r")) == NULL) err(1, "%s", path); line_count = found_scheme = found_scheme_name = 0; bzero(&buf, sizeof(buf)); while (fgets(buf, sizeof(buf), conf) != NULL) { line_count++; if (buf[0] == '#') /* comment */ continue; p = &buf[strlen(buf)]; while (isspace(*(--p))) /* take care of whitespace */ if (p >= buf) *p = '\0'; if (strlen(buf) == 0) /* empty line */ continue; if (buf[0] == '[') { if (found_scheme) { found_scheme = 0; break; } p = &buf[1]; pp = strchr(p, ']'); if (!pp) errx(1, "Configuration parse error: Line %d\n", line_count); *pp = '\0'; /* Convert name and p to upper case since scheme * names should not be case-sensetive. *//* up(name); up(p);*/ if (!strcmp(p, name)) { if (!found_scheme) { strlcpy(s.name, name, sizeof(s.name)); found_scheme = 1; found_scheme_name = 1; continue; } } } if (found_scheme) { if (parse_scheme(buf) == -1) errx(1, "Configuration parse error: Line %d\n", line_count); } } if (!found_scheme_name) errx(1, "No such scheme: %s", name); fclose(conf); memcpy(sch, &s, sizeof(struct scheme));}/* find a field of the scheme, return the position */intget_field(char *field_name){ int i; for (i = 0; fld[i].f_name; i++) { if (!strcmp(field_name, fld[i].f_name)) return i; } return -1;}/* parse a scheme-line, return 0 on success */intparse_scheme(char *buf){ int pos; char field_buf[MAX_FIELD]; char *p; const char *errstr; strlcpy(field_buf, buf, sizeof(field_buf)); p = strchr(field_buf, '='); if (!p) return -1; *p = '\0'; if ((pos = get_field(field_buf)) == -1) { fprintf(stderr, "No such value: %s\n", field_buf); return -1; } switch (fld[pos].type) { case PERCENT: p = strip(buf); if (!p) return -1; *(int *)fld[pos].ptr = (int)strtonum(p, 0, 100, &errstr); if (errstr) { fprintf(stderr, "%s: %s\n", p, errstr); return -1; } return 0; case SLEEPVAL: p = strip(buf); if (!p) return -1; *(int *)fld[pos].ptr = (int)strtonum(p, 0, 1000000, &errstr); if (errstr) { fprintf(stderr, "%s: %s\n", p, errstr); return -1; } return 0; default: return -1; }}/* strip bla= from bla=foo */char *strip(char *buf) { char *p; p = strchr(buf, '='); if (p && (*(++p) != '\0')) return(p); return NULL;}/* Convert scheme names to upper case before comparing *//*voidup(char *nam){ int c; while((c = *nam) != 0) *nam++ = toupper(c);}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -