Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
sqwave.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <string.h>
00014
00015 int main(argc, argv)
00016 int argc;
00017 char **argv;
00018 {
00019 int narg ;
00020 int num_on=0 , num_off=0 , num_len=0 , num_cycles=0 , num_init=0 ,
00021 num_on_kill=0 , num_off_kill=0 , num_init_kill=0 ;
00022 static char fname[128] = "REF.ts" ;
00023 FILE *sqfile ;
00024 int ii , nout ;
00025
00026
00027
00028 #define ERROR {printf("FatalError\n");exit(1);}
00029
00030 if( argc < 2 || strncmp(argv[1],"-help",2) == 0 ){
00031 fprintf( stderr ,
00032 "Usage: %s [-on #] [-off #] [-length #] [-cycles #]\n" , argv[0] ) ;
00033 fprintf( stderr ,
00034 " [-init #] [-onkill #] [-offkill #] [-initkill #] [-name name]\n");
00035 ERROR ;
00036 }
00037
00038 narg = 1 ;
00039 do {
00040
00041 if( strncmp(argv[narg],"-initkill",7) == 0 ){
00042 num_init_kill = strtol( argv[++narg] , NULL , 10 ) ;
00043 if( num_init_kill < 0 ){
00044 fprintf(stderr,"num_init_kill = %d\n",num_init_kill) ;
00045 ERROR ;
00046 }
00047 continue ;
00048 }
00049
00050 if( strncmp(argv[narg],"-onkill",5) == 0 ||
00051 strncmp(argv[narg],"-killon",7) == 0 ){
00052
00053 num_on_kill = strtol( argv[++narg] , NULL , 10 ) ;
00054 if( num_on_kill < 0 ) ERROR ;
00055 continue ;
00056 }
00057
00058 if( strncmp(argv[narg],"-offkill",6) == 0 ||
00059 strncmp(argv[narg],"-killoff",8) == 0 ){
00060
00061 num_off_kill = strtol( argv[++narg] , NULL , 10 ) ;
00062 if( num_off_kill < 0 ) ERROR ;
00063 continue ;
00064 }
00065
00066 if( strncmp(argv[narg],"-on",3) == 0 ){
00067 num_on = strtol( argv[++narg] , NULL , 10 ) ;
00068 if( num_on <= 0 ) ERROR ;
00069 continue ;
00070 }
00071
00072 if( strncmp(argv[narg],"-off",4) == 0 ){
00073 num_off = strtol( argv[++narg] , NULL , 10 ) ;
00074 if( num_off <= 0 ) ERROR ;
00075 continue ;
00076 }
00077
00078 if( strncmp(argv[narg],"-length",4) == 0 ){
00079 num_len = strtol( argv[++narg] , NULL , 10 ) ;
00080 if( num_len <= 0 ) ERROR ;
00081 continue ;
00082 }
00083
00084 if( strncmp(argv[narg],"-cycles",4) == 0 ){
00085 num_cycles = strtol( argv[++narg] , NULL , 10 ) ;
00086 if( num_cycles <= 0 ) ERROR ;
00087 continue ;
00088 }
00089
00090 if( strncmp(argv[narg],"-init",4) == 0 ){
00091 num_init = strtol( argv[++narg] , NULL , 10 ) ;
00092 if( num_init < 0 ) ERROR ;
00093 continue ;
00094 }
00095
00096 if( strncmp(argv[narg],"-name",4) == 0 ){
00097 strcpy( fname , argv[++narg] ) ;
00098 continue ;
00099 }
00100
00101 fprintf( stderr , "illegal option in sqwave: %s\n" , argv[narg] ) ;
00102 ERROR ;
00103
00104 } while( ++narg < argc ) ;
00105
00106
00107
00108 if( num_on <= 0 && num_off <= 0 ) ERROR ;
00109
00110 if( num_on <= 0 ){
00111 num_on = num_off ;
00112 } else if( num_off <= 0 ){
00113 num_off = num_on ;
00114 }
00115
00116 if( num_on <= num_on_kill ) ERROR ;
00117 if( num_off <= num_off_kill ) ERROR ;
00118 if( num_init < num_init_kill ) ERROR ;
00119
00120 if( num_len <= 0 ){
00121 if( num_cycles > 0 ){
00122 num_len = num_cycles * ( num_on + num_off ) ;
00123 if( num_init > 0 ) num_len = num_len + num_init ;
00124 } else {
00125 ERROR ;
00126 }
00127 }
00128
00129 if( strncmp(fname,"stdout",6) == 0 || fname[0] == '\0' || fname[0] == '-' ){
00130 sqfile = stdout ;
00131 } else {
00132 sqfile = fopen( fname , "w" ) ;
00133 if( sqfile == NULL ) ERROR ;
00134 }
00135
00136
00137
00138 nout = 0 ;
00139
00140 #define POFF {fprintf(sqfile,"0\n") ;nout++;if(nout>=num_len)goto DONE;}
00141 #define PON {fprintf(sqfile,"10\n") ;nout++;if(nout>=num_len)goto DONE;}
00142 #define PKIL {fprintf(sqfile,"99999\n");nout++;if(nout>=num_len)goto DONE;}
00143
00144 for( ii=0 ; ii < num_init ; ii++ ){
00145 if( ii < num_init_kill ) PKIL else POFF
00146 }
00147
00148 do{
00149
00150 for( ii=0 ; ii < num_on ; ii++ ){
00151 if( ii < num_on_kill ) PKIL else PON
00152 }
00153
00154 for( ii=0 ; ii < num_off; ii++ ){
00155 if( ii < num_off_kill ) PKIL else POFF
00156 }
00157
00158 } while( 1 ) ;
00159
00160 DONE:
00161 if( sqfile != stdout ) fclose( sqfile ) ;
00162 printf("%s\n" , fname ) ;
00163 exit(0) ;
00164 }