📄 g_nnfsrc.c
字号:
fprintf(g, "\nWRITE_MEDIUM=") ; for(i=0;;i++) if( strcmp(vt[i].name, "WRITE_MEDIUM") == 0 ) { g_Write_Content(g, vt[i].content) ; break ; } fprintf(g, "\nEJECT=") ; for(i=0;;i++) if( strcmp(vt[i].name, "EJECT") == 0 ) { g_Write_Content(g, vt[i].content) ; break ; } fprintf(g, "\n;;\n\n") ;}/* * This function get variable value of any case in the nnfsrc * or save the varisble values if "g" is non NULL */void g_Nnfsrc_Action(char *name, g_Nnfsrc_Var *vt, g_Boolean read_nnfsrc, char *in_case, g_Boolean *in_case_found, char ***cases, int *nb_cases, FILE *g, const char *hostname ){ FILE *f ; int i ; int c ; char named[G_LINE_CHUNK] ; char *current_case ; char *tmp ; g_Boolean insert ; if ( read_nnfsrc ) f = g_fopen(name, "r") ; current_case = NULL ; *in_case_found = g_False ; *nb_cases = 0 ; G_MALLOC(*cases,1) ; insert = g_False ; if ( g == NULL ) for(i=0;vt[i].name;i++) { g_Free(vt[i].content) ; vt[i].content = g_Save_String("") ; } do { /* * Jump start of line */ start_of_line: do { c = getc(f) ; if ( c>=0 && g ) putc(c, g) ; } while( c==' ' || c=='\t' || c=='\n' ) ; /* * Read until '=' or ')' */ i = 0 ; named[i++] = c ; for(;!feof(f);) { switch( named[i-1] ) { case '=': named[i-1] = '\0' ; tmp = g_Read_Content(f) ; if ( current_case ) if ( strcmp(current_case, in_case) ) { if ( g ) g_Write_Content(g, tmp) ; goto jump_over ; } for(i=0;vt[i].name;i++) if ( strcmp(named, vt[i].name) == 0 ) break ; if ( vt[i].name==NULL ) { g_Printf("NNFS: Variable name mispelled? (%s)\n", named) ; if ( g ) g_Write_Content(g, tmp) ; goto jump_over ; } if ( g ) { if ( current_case ) g_Write_Content(g, tmp) ; else g_Write_Content(g, vt[i].content) ; } g_Free(vt[i].content) ; vt[i].content = tmp ; goto jump_over ; case ')': named[i-1] = '\0' ; current_case = g_Save_String(named) ; if ( strcmp(in_case, current_case) == 0 ) { if ( g ) { /* Do not store this old value */ fseek(g, -i-2, SEEK_CUR) ; fgets(named, sizeof(named), f) ; for(;;) { if ( fgets(named, sizeof(named), f) == NULL ) return ; for(i=0; named[i]; i++) if ( named[i]!=' ' && named[i]!='\t' && named[i]!='\n' ) break ; if ( named[i] == ';' && named[i+1] == ';' ) break ; } do i = getc(f) ; while( i==' ' || i=='\n' || i=='\t' ) ; ungetc(i, f) ; goto jump_over ; } else *in_case_found = g_True ; } i = 0 ; break ; case ';': c = getc(f) ; if ( c>=0 && g ) putc(c, g) ; if ( c==';' ) { if ( current_case ) { (*nb_cases)++ ; G_REALLOC(*cases, *nb_cases) ; (*cases)[*nb_cases-1] = current_case ; current_case = NULL ; } goto jump_over ; } if ( c=='\n' ) goto start_of_line ; goto jump_over ; case ' ': named[i] = '\0' ; if ( g /* && *in_case_found == g_False */ && strncmp(named, "case", 4) == 0 ) { fgets(named, sizeof(named), f) ; fprintf(g, "%s", named) ; g_Nnfsrc_Insert_Case(g, hostname, vt) ; goto start_of_line ; } case '(': case '\t': goto jump_over ; case '\n': goto start_of_line ; case '"': i-- ; do { c = getc(f) ; named[i++] = c ; if ( c>0 && g ) putc(named[i-1], g) ; } while(named[i-1]!='"' && !feof(f)) ; if ( feof(f) ) { g_Printf("Unterminated '\"' in %s\n", name) ; return ; } i-- ; break ; } c = getc(f) ; named[i++] = c ; if ( c>0 && g ) putc(named[i-1], g) ; } /* * */ jump_over: while( (c=getc(f)) != '\n' ) { if ( c>=0 && g ) putc(c, g) ; if ( feof(f) ) break ; } if ( g && c == '\n' ) putc('\n', g) ; } while(!feof(f)) ;}/* * Compile configuration */void G_FUNCTION(g_Compile_Config, (g_Nnfsrc_Var *v, g_Config *config))char *name ;char *(*choices)[2] ;void *ptr ;int i, j ;char *t ;for( i=0 ; v[i].name ; i++) { ptr = (char*)config + v[i].offset_in_g_config ; name = v[i].name ; t = v[i].content ; switch(v[i].type) { case g_Var_String: case g_Var_Big_String: case g_Var_Filter: *(char**)ptr = g_Save_String(t) ; break ; case g_Var_Choice: choices = v[i].choices ; *(int*)ptr = 1 ; for(j=0;j<choices[j][0][0];j++) if ( strcmp(choices[j][0], t) == 0 ) { *(int*)ptr = j ; break ; } if ( choices[j][0][0] == '\0' ) { g_Printf("NNFS: Choice `%s' not possible for `%s'\n", t, name) ; g_Printf("NNFS: Accepted values are:\n") ; for(j=0;j<choices[j][0][0];j++) g_Printf("NNFS: %s (%s)\n", choices[j][0], choices[j][1]) ; } break ; case g_Var_Integer: case g_Var_Boolean: if ( t[0] ) { if ( strcmp(t,"yes")==0 || strcmp(t,"y")==0 || strcmp(t,"1")==0 || strcmp(t,"true")==0 ) *(int*)ptr = 1 ; else *(int*)ptr = atoi(t) ; } break ; }}G_RETURN(;) ; }/* * */void G_FUNCTION(g_Read_Config, (g_Config *config, const char *hostname))g_Read_Config_Char(config->nnfsrc, g_Nnfsrc_Var_Table(), hostname) ;g_Compile_Config(g_Nnfsrc_Var_Table(), config) ;G_RETURN(;) ;}/* * */char * G_FUNCTION(g_Create_Nnfsrc,(char *home, g_Boolean *created))char *nnfsrc ;FILE *f ;nnfsrc = g_Nnfsrc(home) ;f = fopen(nnfsrc,"r") ;if ( f ) { fclose(f) ; *created = g_False ; G_RETURN(nnfsrc) ; }if ( errno != ENOENT ) { perror(nnfsrc) ; G_EXIT(1) ; }f = g_fopen(nnfsrc,"w") ;#include "nnfsrc"g_fclose(f) ;*created = g_True ;G_RETURN(nnfsrc) ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -