crabs_makefile-5.2 ©2003-2008 - Christophe Cazajus (crabs-mettre_le_signe_at-crabs-world.com)
1 /* 2 **============================================================================= 3 ** crabs_makefile-5.2 : Makefile pour developpement multi-OS, multi-variants 4 ** Copyright (C) 2003-2008 : Christophe Cazajus (crabs-mettre_le_signe_at-crabs-world.com) 5 ** 6 ** Ce source fait partie d'un projet logiciel libre. Vous pouvez le distribuer 7 ** et/ou le modifier en respectant les termes de la GNU General Public License 8 ** version 2 ou (suite a votre propre choix) une version ulterieure. 9 ** 10 ** Ce programme est distribue dans l'espoir qu'il puisse etre utile, mais 11 ** sans aucune garantie, meme si il est associe a un produit qui vous en 12 ** propose une. Conformez-vous a la GNU General Public License pour avoir 13 ** plus de precisions. 14 ** 15 ** L'auteur ne peut etre tenu responsable de l'utilisation faite des 16 ** composantes associees a ce projet (en partie ou dans leur totalite). 17 ** 18 ** Une copie du fichier de la GNU GPL est fournie dans le repertoire DOC 19 ** de ce projet sous le nom gnu_gpl.txt 20 ** 21 **============================================================================= 22 */ 23 #include <sys/types.h> 24 #include <sys/stat.h> 25 #include <fcntl.h> 26 #include <stdlib.h> 27 #include <stdio.h> 28 #include <string.h> 29 #include <unistd.h> 30 #include <errno.h> 31 #include <libintl.h> 32 #include <locale.h> 33 34 struct PGPL { 35 char auteur [128] ; 36 char desc [128] ; 37 char produit [32] ; 38 char version [16] ; 39 char annee [16] ; 40 char email [128] ; 41 char path [2048] ; 42 int store ; 43 } pgpl ; 44 45 struct { 46 char* name ; 47 char* shname ; 48 char* pgpl ; 49 size_t len ; 50 } field[] = { 51 { "auteur", "PGPL_AUTEUR", pgpl.auteur, 128 }, 52 { "desc", "PGPL_DESC", pgpl.desc, 128 }, 53 { "produit", "PGPL_PRODUIT", pgpl.produit, 32 }, 54 { "version", "PGPL_VERSION", pgpl.version, 16 }, 55 { "annee", "PGPL_ANNEE", pgpl.annee, 16 }, 56 { "email", "PGPL_EMAIL", pgpl.email, 128 }, 57 { 0, 0, 0, 0 } 58 } ; 59 60 #define E(a...) fprintf( stderr, a ) 61 void usage( char*pgm, char* msg ) 62 { 63 E(gettext("%s: ERROR: %s\n"),pgm,msg); 64 E( " %s -f|-p path [cmd] [...]\n" ,pgm); 65 E(gettext(" where cmd in :\n")); 66 E( " -set auteur|desc|produit|version|annee|email value\n" ); 67 E( " -get ALL|auteur|desc|produit|version|annee|email\n" ); 68 E(gettext(" use :\n")) ; 69 E(gettext(" ALL : display all informations on the project\n")) ; 70 E(gettext(" auteur : display/set author\n")) ; 71 E(gettext(" desc : display/set description\n")) ; 72 E(gettext(" produit: display/set product name (short)\n")) ; 73 E(gettext(" version: display/set release\n")) ; 74 E(gettext(" annee : display/set years\n")) ; 75 E(gettext(" email : display/set email\n")) ; 76 exit( 1 ) ; 77 } 78 79 void set( int argc, char** argv, int* pos ) ; 80 void get( int argc, char** argv, int* pos ) ; 81 void load( char* path, int is_project ) ; 82 void save() ; 83 84 int main( int argc, char** argv ) 85 { 86 char* tmp ; 87 int modeprojet ; 88 int pos = 3 ; 89 90 setlocale( LC_ALL, "" ) ; textdomain( "libc" ) ; 91 if ( getenv( "CRABS_LOCALE") ) 92 { 93 bindtextdomain( "crabs_makefile", getenv( "CRABS_LOCALE" ) ) ; 94 textdomain( "crabs_makefile" ) ; 95 } 96 97 if ( argc < 4 ) usage( *argv, gettext("missing arguments") ) ; 98 tmp = argv[1] ; 99 if (!strcmp( tmp, "-f" ) ) modeprojet = 0 ; 100 else if (!strcmp( tmp, "-p" ) ) modeprojet = 1 ; 101 else usage( *argv, gettext("-f or -p") ) ; 102 load( argv[2], modeprojet ) ; 103 104 while( pos < argc ) 105 { 106 tmp = argv[pos] ; 107 if ( strcmp( tmp, "-get" ) == 0 ) get( argc, argv, &pos ) ; 108 else if ( strcmp( tmp, "-set" ) == 0 ) set( argc, argv, &pos ) ; 109 else usage( *argv, gettext("cmd must be -get or -set") ) ; 110 } 111 save() ; 112 return 0 ; 113 } 114 115 void get( int argc, char** argv, int* pos ) 116 { 117 char* tmp ; 118 int ok = 0 ; 119 int f ; 120 121 if ( (*pos)+1 >= argc ) usage( *argv, gettext("missing var with -get") ) ; 122 tmp = argv[(*pos)+1] ; 123 for( f=0; field[f].name; f++ ) 124 { 125 if ( strcmp( tmp, "ALL" ) && strcmp( tmp, field[f].name ) ) continue ; 126 printf( "%s='%s'\n", field[f].shname, field[f].pgpl ) ; 127 ok = 1 ; 128 } 129 if ( !ok ) usage( *argv, gettext("-get: unknown var") ) ; 130 *pos = (*pos) + 2 ; 131 } 132 133 void set( int argc, char** argv, int* pos ) 134 { 135 char* tmp ; 136 int ok = 0 ; 137 int f ; 138 139 if ( (*pos)+2 >= argc ) 140 usage( *argv, gettext("missing var or val with -set") ) ; 141 tmp = argv[(*pos)+1] ; 142 for( f=0; field[f].name; f++ ) 143 { 144 if ( strcmp( tmp, field[f].name ) ) continue ; 145 strncpy( field[f].pgpl, argv[(*pos)+2], field[f].len-1 ) ; 146 field[f].pgpl[field[f].len-1] = 0 ; 147 ok = 1 ; 148 } 149 if ( !ok ) usage( *argv, gettext("-set: unknown var") ) ; 150 *pos = (*pos) + 3 ; 151 pgpl.store = 1 ; 152 } 153 154 void load( char* path, int is_project ) 155 { 156 struct stat st ; 157 int in ; 158 char* cnt ; 159 char* cnt_end ; 160 char* line_begin ; 161 char* line_end ; 162 int f ; 163 164 strcpy( pgpl.path, path ) ; pgpl.store = 0 ; 165 if ( is_project ) strcat( pgpl.path, "/.projet/info.gpl" ) ; 166 167 if ( stat( pgpl.path, &st ) == -1 ) 168 { 169 fprintf( stderr, "stat(%s):%s\n", pgpl.path, strerror(errno) ) ; 170 exit( 1 ) ; 171 } 172 if ( ( in = open( pgpl.path, O_RDONLY ) ) == -1 ) 173 { 174 fprintf( stderr, "open(%s):%s\n", pgpl.path, strerror(errno) ) ; 175 exit( 1 ) ; 176 } 177 if ( ( cnt = malloc( st.st_size ) ) == 0 ) 178 { 179 fprintf( stderr, "malloc(%u):%s\n", st.st_size, strerror(errno) ) ; 180 close( in ) ; 181 exit( 1 ) ; 182 } 183 if ( read( in, cnt, st.st_size ) != st.st_size ) 184 { 185 fprintf( stderr, "read(%s):%s (not same stat size)\n", 186 pgpl.path, strerror(errno) ) ; 187 free( cnt ) ; close( in ) ; 188 exit( 1 ) ; 189 } 190 // init with empty string 191 for( f=0; field[f].name; f++ ) *field[f].pgpl=0 ; 192 // 1 line per var 193 line_begin = cnt ; cnt_end = cnt + st.st_size ; 194 for( f=0,line_end=cnt; line_end<cnt_end; line_end++ ) 195 { 196 if ( *line_end != '\n' ) continue ; 197 if ( field[f].name == 0 ) continue ; 198 199 *line_end = 0 ; 200 strncpy( field[f].pgpl, line_begin, field[f].len-1 ) ; 201 field[f].pgpl[field[f].len-1] = 0 ; 202 f++ ; line_begin = line_end + 1 ; 203 } 204 close( in ) ; 205 } 206 207 void save() 208 { 209 int out ; 210 int f ; 211 if ( !pgpl.store ) return ; 212 if ( ( out = open( pgpl.path, O_WRONLY|O_TRUNC|O_CREAT, 0666 ) ) == -1 ) 213 { 214 fprintf( stderr, "open(%s):%s\n", pgpl.path, strerror(errno) ) ; 215 exit( 1 ) ; 216 } 217 for( f=0; field[f].name; f++ ) 218 { 219 size_t len = strlen( field[f].pgpl ) ; 220 field[f].pgpl[ len ] = '\n' ; 221 write( out, field[f].pgpl, len+1 ) ; 222 } 223 close( out ) ; 224 } 225
Date de génération : 12/09/2009 14:15