コード例 #1
0
ファイル: print.c プロジェクト: macee/tux_15
int main(void){
  char line[100];
  uint8_t i;
  uint8_t num_fields;
  uint8_t num_char_in_field;
  struct timeval time_now;

    while(fgets(line, 100, stdin) != NULL){

        // Linux time stamp
            gettimeofday(&time_now, NULL);

        /* remove newline, if present */
            i = strlen(line)-1;
            if( line[ i ] == '\n')
            line[i] = '\0';

        /* FIXME after you have program working */
            sprintf(line, "%s", "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47");


        num_fields = line_parser(line, ',');


        /* Operate on the GGA strings */
            get_field(line, 1);
            if(strncmp(line, "$GPGGA", 6))
                continue;


        /* Verify line has correct number of fields*/
            if (num_fields != 15)
                continue;


        /* FIXME after you have program working */
            printf("\nThe string \"%s\" contains %d fields.  The individual fields are: \n", line, num_fields);
            for (i = 1; i <= num_fields; i++){
                num_char_in_field = get_field(line, i);
                fprintf(stdout, "\tfield %d with length %d = %s\n",i, num_char_in_field, line );
            }

    // fprintf(stdout, "%ld, %s\n", time_now.tv_sec, line);

    }
    return 0;
}
コード例 #2
0
ファイル: main.c プロジェクト: opensourcegrappler/mct
int main(int argc, char *argv[])
{

    FILE *fh;

    fh = fopen(argv[1],"r");

    int datarate;
    
    if (argv[2]) datarate = atoi(argv[2]);
    else datarate = 5;
   
    char * line = NULL;
    size_t len = 0;
    ssize_t read;

    int line_retval;

    int count =0;
    double degrees = 0;

    typedef struct datastr {
        double proll4;
        double proll3;
        double proll2;
        double proll1;
        double roll;
        double roll1;
        double roll2;
        double roll3;
        double roll4;
    } rollstr;

    rollstr R;

    //lpf phase delay, 4 frames = N/2 from FIR filter
    int lpf_delay = 4;
    
    float a0 = 0.227272727;
    float a1 = 0.1966520727;
    float a2 = 0.12272727272;
    float a3 = 0.0488024;
    float a4 = 0.018181818;

    //the current and previous datapoints
    inertial current_t;
    inertial previous_t;

    
    while((read = getline(&line, &len, fh)) != -1)
    {
        //call the line parser
        line_retval = line_parser(line,&current_t);
        
        if (!line_retval)
        {
            current_t.roll = roll_calc(&current_t,&previous_t,datarate);
            
            R.roll4 = current_t.roll;

            //do filtering here
            degrees = (a0*R.roll) +
                (a1*R.roll1)  +
                (a1*R.proll1) +
                (a2*R.roll2)  +
                (a2*R.proll2) +
                (a3*R.roll3)  +
                (a3*R.proll3) +
                (a4*R.roll4)  +
                (a4*R.proll4);
                
            printf("%d %f %f %f\n",count,current_t.roll,previous_t.roll,degrees);

            //after the initial delay draw things
            if (count > lpf_delay)
            {
                draw_roll_gauge(degrees,count-lpf_delay-1,datarate);
            }
            
            //increment the frame counter
            count++;
            
            //shuffle roll values along the buffer
            R.proll4 = R.proll3;
            R.proll3 = R.proll2;
            R.proll2 = R.proll1;
            R.proll1 = R.roll;
            R.roll = R.roll1;
            R.roll1 = R.roll2;
            R.roll2 = R.roll3;
            R.roll3 = R.roll4;
            
            //the current datapoint becomes the previous datapoint
            previous_t = current_t;
        }
    }
    return 0;
}
コード例 #3
0
ファイル: myshell.c プロジェクト: nkg2502/myshell
int main(int argc, char *argv[], char *envp[])
{
	char line_buffer[MAX_BUFFER]; // 사용자로부터 입력을 받음
	char **command;		// parsing된 line에 대한 포인터
	int flag_fa;		// file append에 대한 플래그
	int flag_bg;		// background excution에 대한 플래그
	FILE *fp = stdout;	// redirection file pointer for internal command

	// ls -al을 저장할 공간과
	// less /<fullpath>/readme를 저장할 공간이 필요해
	char *alias[] = {
		"ls",
		"-al",
		"less"
	};

	char path_readme[MAX_BUFFER]; // fullpath를 저장할 공간 readme 파일을 위한 것임

	// 쉘을 실행하는 방법은 3가지
	// 1. myshell이 있는 디렉토리에서 path없이 실행
	// 	ex) ./myshell
	// 2. 외부 디렉토리에서 myshell을 경로로 실행
	// 	ex) /home/test/myshell
	// 3. myshell이 있는 디렉토리가 path가 있어서 실행
	// 	ex) myshell
	// 	이런 경우에는 executable한 path를 어떻게 알아내냐?
	// 아무래도 생각하기에는
	// 1번일 가능성이 높은 것으로 사료됨
	// 아래의 코드는 1번을 고려해서 작성
	strncpy(line_buffer, getcwd(NULL, 0), MAX_BUFFER); // SHELL
	strncpy(path_readme, line_buffer, MAX_BUFFER); // readme

	strncat(line_buffer, "/myshell", MAX_BUFFER); // SHELL
	strncat(path_readme, "/readme", MAX_BUFFER); // readme

	setenv("SHELL", line_buffer, 1); // 1. ix.

	if(argv[1]) // for batchfile
		if(!freopen(argv[1], "r", stdin))
			exit(STDIN_FAIL);

	while(!feof(stdin)) {

		if(argc < 2) // batchfile에서는 프롬프트 안나오게 하기 위해서
			printf("%s>", getcwd(NULL, 0));

		if(fgets(line_buffer, MAX_BUFFER, stdin)) {

			flag_fa = OFF;
			flag_bg = OFF;

			if(fp != stdout)
				fclose(fp);
			
			command = line_parser(line_buffer, &flag_fa, &flag_bg);

			// 이쪽 아래로는 line_buffer를
			// strcpy나 strcat 용도로 사용해도 괜찮음
		
			if(command[CMD_RUN]) {

				// internal command alias
				// 왜 내부 커맨드 + .. 하면 디렉토리가 변경되는거지? // 파싱을 잘못해섴
				// NULL을 마지막에 넣어주지 않았기 때문임
				
				// 내부 커맨드의 리다이렉션을 위한 파일 오픈
				if(NULL == (fp = fopen(command[CMD_STDOUT], flag_fa ? "a" : "w")))
					fp = stdout;

				//pstatus(command, fp, flag_fa, flag_bg);
	
				switch(internal_cmd(command[CMD_RUN])) {
					case i_cd: // OLDPWD도 갱신해야하나? 우선은 갱신해놓기로 함
						{
							// 물론 아래의 선언은 필요하지 않다
							// 그렇지만 일관성 측면에서는 더 좋은거 같기도 하다
							char *target_dir = command[CMD_RUN + 1];
							
							if(target_dir)
								if(chdir(target_dir))
									printf("%s : No such directory\n", target_dir);
								else {
									setenv("OLDPWD", getenv("PWD"), 1);
									setenv("PWD", getcwd(NULL, 0), 1);
								}
							else
								printf("%s\n", getcwd(NULL, 0));
						} continue;
					case i_dir:
						// internal command and alias이기 때문에
						// ls -al로 실행하면 될꺼 같다
						// 문제는 뭐냐면 ls a b c 이런것도 가능하다는거다...
						// arg는 최대 64개를 넘지 않는다고 가정한다
						{
							int arg_counter = CMD_RUN;
							while(command[arg_counter++])
								;
							while(arg_counter-- > CMD_RUN + 2)
								command[arg_counter] = command[arg_counter - 1];

							command[CMD_RUN] = alias[0]; // ls
							command[CMD_RUN + 1] = alias[1]; // -al

							//pstatus(command, fp, flag_fa, flag_bg);

						} break; 
					case i_environ:
						{
							char **env = envp;
							while(*env)
								fprintf(fp, "%s\n", *env++);
							fprintf(fp, "\n");
						} continue;
					case i_clr:
						{
							fprintf(stdout, "");
							fflush(stdout);
						} continue;
					case i_echo:
						{
							char **comment = command + CMD_RUN;
							while(*++comment)
								fprintf(fp, "%s ", *comment);
							fprintf(fp, "\n");
						} continue;
					case i_help:
						{
							// index 접근법을 좀 바꿀까?
							command[CMD_RUN] = alias[2]; // less
							command[CMD_RUN + 1] = path_readme;
							command[CMD_RUN + 2] = NULL;
						} break;
					case i_pause:
						{
							pdebug("pause");
							struct termios term;
							// echo off
							tcgetattr(STDIN_FILENO, &term);
							term.c_lflag ^= ECHO;
							tcsetattr(STDIN_FILENO, TCSANOW, &term);

							printf("Press Enter to continue...\n");
							fgets(line_buffer, MAX_BUFFER, stdin);

							// echo on
							tcgetattr(STDIN_FILENO, &term); // 이건 없어도 작동할듯?
							term.c_lflag ^= ECHO;
							tcsetattr(STDIN_FILENO, TCSANOW, &term);

						} continue;
					case i_myshell: // for batchfile
						{
							command[CMD_RUN] = getenv("SHELL");
						} break;
					case i_quit:
						exit(0);
				}

				fork_exec(command, flag_fa, flag_bg);
			}
		}
	}

	exit(0);
	// return 0도 물론 가능하지만 
	// forking과 exec를 하므로
	// 좀더 안전하지 않을까?
}