"Fossies" - the Fresh Open Source Software Archive

Member "delegate9.9.13/include/mkcpp.c" (4 Sep 2010, 5177 Bytes) of package /linux/misc/old/delegate9.9.13.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mkcpp.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * 041129 ysato@delegate.org
    3  */
    4 #include <ctype.h>
    5 #include <stdio.h>
    6 #include <string.h>
    7 
    8 #if defined(__linux__) /* RedHat6 */ \
    9  || defined(__APPLE__) /* gcc4.0 */ \
   10  || defined(__osf__) /* DEC ALPHA */ \
   11  || defined(__CYGWIN__)
   12 
   13 #include <sys/time.h>
   14 #endif
   15 #include <time.h>
   16 #include <sys/types.h>
   17 #include <sys/stat.h>
   18 
   19 #ifndef _MSC_VER
   20 #include <stdlib.h>
   21 #endif
   22 
   23 #ifdef _MSC_VER
   24 #include <windows.h>
   25 #include <winbase.h>
   26 static void utime2ftime(struct timeval *tv,FILETIME *ftime)
   27 {   int year,mon,day,hour,min,sec;
   28     struct tm *tm;
   29     WORD ddate,dtime;
   30     struct tm tm0;
   31     time_t tsec;
   32 
   33     tsec = tv->tv_sec;
   34     tm = gmtime(&tsec);
   35     /*
   36     tm = gmtime(&tv->tv_sec);
   37     */
   38     if( tm == 0 ){
   39         memset(&tm0,0,sizeof(tm0));
   40         tm = &tm0;
   41     }
   42 
   43     year = tm->tm_year + 1900 - 1980;
   44     mon  = tm->tm_mon  +  1;
   45     day  = tm->tm_mday;
   46     hour = tm->tm_hour;
   47     min  = tm->tm_min;
   48     sec  = tm->tm_sec / 2;
   49     ddate = (year <<  9) | (mon << 5) | day;
   50     dtime = (hour << 11) | (min << 5) | sec;
   51     DosDateTimeToFileTime(ddate,dtime,ftime);
   52 }
   53 int utimes(const char *path,struct timeval *tvp)
   54 {   HANDLE fh;
   55     FILETIME atime,mtime;
   56     BOOL ok;
   57     struct tm *tm;
   58 
   59     fh = CreateFile(path,GENERIC_READ|GENERIC_WRITE,
   60         0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
   61     if( fh == NULL )
   62         return -1;
   63 
   64     utime2ftime(&tvp[0],&atime);
   65     utime2ftime(&tvp[1],&mtime);
   66 
   67     ok = SetFileTime(fh,NULL,&atime,&mtime);
   68     CloseHandle(fh);
   69     if( ok )
   70         return 0;
   71     else    return -1;
   72 }
   73 #endif
   74 
   75 
   76 #include "../putsigned.c"
   77 #ifndef _PUTSIGNED_C_
   78 int putsigned(FILE *in,FILE *out,char *line,int didsign){
   79     return 0;
   80 }
   81 #endif
   82 
   83 static int p2i(const void *p){
   84     union {
   85         const void *p;
   86         int i;
   87     } u;
   88     u.p = p;
   89     return u.i;
   90 }
   91 
   92 static FILE *include_inline(FILE *out,const char *line){
   93     char file[1024];
   94     FILE *fp;
   95     file[0] = 0;
   96     sscanf(line,"#include \"%[^\"]",file);
   97     if( fp = fopen(file,"r") ){
   98         return fp;
   99     }
  100     return 0;
  101 }
  102 static int strheq(const char *str,const char *hs){
  103     if( strncmp(str,hs,strlen(hs)) == 0 )
  104         return strlen(hs);
  105     return 0;
  106 }
  107 typedef struct {
  108     FILE *fp_fp;
  109     int   fp_li;
  110 } filePos;
  111 int main(int ac, char *av[]){
  112     FILE *in = stdin;
  113     FILE *out = stdout;
  114     char orig[1024];
  115     char line[1024],*lp,ch,arg[1024];
  116     int nlen;
  117     struct stat st;
  118     int mtime = -1;
  119     const char *src = "--stdin";
  120     const char *dst = "--stdout";
  121     int lni,lno;
  122     FILE *in1;
  123     filePos fP = {0,0};
  124 
  125     int ai;
  126     int dosign = 0;
  127     int didsign = 0;
  128 
  129     for( ai = 1; ai < ac; ai++ ){
  130         if( strcmp(av[ai],"-sign") == 0 ){
  131             dosign = 1;
  132         }
  133     }
  134 
  135     if( 1 < ac ){
  136         if( 2 < ac && strcmp(av[1],av[2]) == 0 ){
  137             sprintf(orig,"%s.orig",av[1]);
  138             in = fopen(orig,"r");
  139             if( in != 0 ){
  140                 fclose(in);
  141             }else{
  142                 rename(av[1],orig);
  143             }
  144             av[1] = orig;
  145         }
  146         in = fopen(av[1],"r");
  147         src = av[1];
  148     }
  149     if( 2 < ac ){
  150         out = fopen(av[2],"w");
  151         dst = av[2];
  152     }
  153     if( src == 0 || dst == 0 ){
  154         fprintf(stderr,"--- mkcpp %X %X\n",p2i(src),p2i(dst));
  155         exit(-1);
  156     }
  157     if( in == 0 || out == 0 ){
  158         fprintf(stderr,"--- mkcpp %X:%s %X:%s\n",p2i(in),src,p2i(out),dst);
  159         exit(-1);
  160     }
  161     if( fstat(fileno(in),&st) == 0 ){
  162         mtime = st.st_mtime;
  163     }
  164 
  165     lni = lno = 0;
  166     while( 1 ){
  167         if( fgets(line,sizeof(line),in) == NULL ){
  168             if( fP.fp_fp ){
  169                 lni = fP.fp_li;
  170                 in = fP.fp_fp;
  171                 fP.fp_fp = 0;
  172                 continue;
  173             }
  174             break;
  175         }
  176         if( dosign ){
  177             didsign = putsigned(in,out,line,didsign);
  178             continue;
  179         }
  180         if( lni != lno ){
  181             fprintf(out,"#line %d\n",lni+1);
  182             lno = lni;
  183         }
  184         if( strncmp(line,"#line ",6) == 0 ){
  185             lni = lno = atoi(line+6);
  186             fprintf(out,"#line %d /*INH*/\n",lni);
  187             continue;
  188         }
  189         if( strncmp(line,"#include",8)==0 && strstr(line,"{inline}") ){
  190             if( in1 = include_inline(out,line) ){
  191                 fprintf(out,"#if 0\n");
  192                 fprintf(out,"%s",line);
  193                 fprintf(out,"#endif\n");
  194                 fprintf(out,"#line 1\n");
  195                 fP.fp_fp = in;
  196                 fP.fp_li = lni;
  197                 in = in1;
  198                 lni = lno = 0;
  199                 continue;
  200             }
  201         }
  202         lni++;
  203         for(lp = line; (ch = *lp) != 0; lp += nlen){
  204             if( line[0] != '#' && (nlen = strheq(lp,"putMssg(")) ){
  205                 fprintf(out,"HTML_putmssg(Conn,");
  206             }else
  207             if( nlen = strheq(lp,"MStr(") ){
  208                 sscanf(lp+nlen,"%[^,)]",arg);
  209                 if( lp[nlen+strlen(arg)] != ',' ){
  210                     fprintf(stderr,"ERROR: %s\n",line);
  211                     return -1;
  212                 }
  213                 fprintf(out,"\n#define %sBASE %s\n\tNStr(",
  214                     arg,arg);
  215                 lno += 2;
  216             }else
  217             if( nlen = strheq(lp,"sprintf(") ){
  218                 sscanf(lp+nlen,"%[^,]",arg);
  219                 fprintf(out,"Xsprintf(AVStr(%s)",arg);
  220                 nlen += strlen(arg);
  221             }else
  222             if( nlen = strheq(lp,"Rsprintf(") ){
  223                 sscanf(lp+nlen,"%[^,]",arg);
  224                 fprintf(out,"XRsprintf(TVSTR(%s)&%s",arg,arg);
  225                 nlen += strlen(arg);
  226             }else{
  227                 if( isalnum(ch) || ch == '_' ){
  228                     while( isalnum(ch) || ch == '_' ){
  229                         putc(ch,out);
  230                         ch = *++lp;
  231                     }
  232                     nlen = 0;
  233                 }else{
  234                     putc(ch,out);
  235                     nlen = 1;
  236                     if( ch == '\n' ){
  237                         lno++;
  238                     }
  239                 }
  240             }
  241         }
  242     }
  243     fclose(out);
  244     if( 0 < mtime ){
  245         struct timeval tv[2];
  246         int nmtime = -1;
  247         if( stat(dst,&st) == 0 ){
  248             nmtime = st.st_mtime;
  249         }
  250         if( dosign ){
  251             /* making gen/*.h (depending mkcpp) later than mkcpp */
  252             mtime = time(0) + 1;
  253         }
  254         printf("-- mkcpp set time: %d -> %d %s\n",nmtime,
  255             mtime,dst);
  256         tv[0].tv_sec = mtime;
  257         tv[0].tv_usec = 0;
  258         tv[1] = tv[0];
  259         utimes(dst,tv);
  260     }
  261     return 0;
  262 }