aboutsummaryrefslogtreecommitdiffstats
path: root/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php
blob: 68e9e6bb863f49981cebca4be30a033f69cb96a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/** 
*
* @package code_sniffer
* @version $Id: $
* @copyright (c) 2007 phpBB Group 
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 
*
*/

/**
* Checks that each source file contains the standard header.
* 
* Based on Coding Guidelines 1.ii File Header.
*
* @package code_sniffer
* @author Manuel Pichler <mapi@phpundercontrol.org>
*/
class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff
{
	/**
	* Returns an array of tokens this test wants to listen for.
	*
	* @return array
	*/
	public function register()
	{
		return array(T_OPEN_TAG);
	}

	/**
	* Processes this test, when one of its tokens is encountered.
	*
	* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
	* @param int				  $stackPtr  The position of the current token
	*										in the stack passed in $tokens.
	*
	* @return null
	*/
	public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
	{
		// We are only interested in the first file comment.
		if ($stackPtr !== 0)
		{
			if ($phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1) !== false)
			{
                return;
			}
		}
		
		// Fetch next non whitespace token
		$tokens = $phpcsFile->getTokens();
		$start  = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);

		// Skip empty files
		if ($tokens[$start]['code'] === T_CLOSE_TAG)
		{
			return;
		}
		// Mark as error if this is not a doc comment
		else if ($start === false || $tokens[$start]['code'] !== T_DOC_COMMENT)
		{
			$phpcsFile->addError('Missing required file doc comment.', $stackPtr);
			return;
		}
		
		// Find comment end token
		$end = $phpcsFile->findNext(T_DOC_COMMENT, $start + 1, null, true) - 1;
		
		// If there is no end, skip processing here
		if ($end === false)
		{
			return;
		}
		
		// List of found comment tags
		$tags = array();
		
		// check comment lines without the first(/**) an last(*/) line
		for ($i = $start + 1, $c = ($end - $start); $i <= $c; ++$i)
		{
			$line = $tokens[$i]['content'];

			// Check that each line starts with a '*'
			if (substr($line, 0, 1) !== '*')
			{
                $message = 'The file doc comment should not be idented.';
				$phpcsFile->addWarning($message, $i);
			}
			else if (preg_match('/^\*\s+@([\w]+)\s+(.*)$/', $line, $match) !== 0)
			{
				$tags[$match[1]] = array($match[2], $i);
			}
		}
		
		// Check that the first and last line is empty
		if (trim($tokens[$start + 1]['content']) !== '*')
		{
			$message = 'The first file comment line should be empty.';
            $phpcsFile->addWarning($message, ($start + 1));			
		}
        if (trim($tokens[$end - $start]['content']) !== '*')
        {
        	$message = 'The last file comment line should be empty.';
            $phpcsFile->addWarning($message, ($end - $start));         
        }
        
        $this->processPackage($phpcsFile, $start, $tags);
        $this->processVersion($phpcsFile, $start, $tags);
        $this->processCopyright($phpcsFile, $start, $tags);
        $this->processLicense($phpcsFile, $start, $tags);
		
        //print_r($tags);
	}
	
	/**
	 * Checks that the tags array contains a valid package tag
	 *
	 * @param PHP_CodeSniffer_File $phpcsFile The context source file instance.
	 * @param integer The stack pointer for the first comment token.
	 * @param array(string=>array) $tags The found file doc comment tags.
	 * 
	 * @return null
	 */
	protected function processPackage(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags)
	{
		if (!isset($tags['package']))
		{
			$message = 'Missing require @package tag in file doc comment.';
			$phpcsFile->addError($message, $ptr);
		}
		else if (preg_match('/^([\w]+)$/', $tags['package'][0]) === 0)
		{
            $message = 'Invalid content found for @package tag.';
            $phpcsFile->addWarning($message, $tags['package'][1]);			
		}
	}
	
	/**
     * Checks that the tags array contains a valid version tag
     *
     * @param PHP_CodeSniffer_File $phpcsFile The context source file instance.
     * @param integer The stack pointer for the first comment token.
     * @param array(string=>array) $tags The found file doc comment tags.
     * 
     * @return null
	 */
	protected function processVersion(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags)
    {
        if (!isset($tags['version']))
        {
            $message = 'Missing require @version tag in file doc comment.';
            $phpcsFile->addError($message, $ptr);
        }
        else if (preg_match('/^\$Id:[^\$]+\$$/', $tags['version'][0]) === 0)
        {
            $message = 'Invalid content found for @version tag, use "$Id: $".';
            $phpcsFile->addError($message, $tags['version'][1]);            
        }
    }
    
    /**
     * Checks that the tags array contains a valid copyright tag
     *
     * @param PHP_CodeSniffer_File $phpcsFile The context source file instance.
     * @param integer The stack pointer for the first comment token.
     * @param array(string=>array) $tags The found file doc comment tags.
     * 
     * @return null
     */
    protected function processCopyright(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags)
    {
        if (!isset($tags['copyright']))
        {
            $message = 'Missing require @copyright tag in file doc comment.';
            $phpcsFile->addError($message, $ptr);
        }
        else if (preg_match('/^\(c\) 2[0-9]{3} phpBB Group\s*$/', $tags['copyright'][0]) === 0)
        {
            $message = 'Invalid content found for @copyright tag, use "(c) <year> phpBB Group".';
            $phpcsFile->addError($message, $tags['copyright'][1]);            
        }
    }
    
    /**
     * Checks that the tags array contains a valid license tag
     *
     * @param PHP_CodeSniffer_File $phpcsFile The context source file instance.
     * @param integer The stack pointer for the first comment token.
     * @param array(string=>array) $tags The found file doc comment tags.
     * 
     * @return null
     */
    protected function processLicense(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags)
    {
    	$license = 'http://opensource.org/licenses/gpl-license.php GNU Public License';
    	
        if (!isset($tags['license']))
        {
            $message = 'Missing require @license tag in file doc comment.';
            $phpcsFile->addError($message, $ptr);
        }
        else if (trim($tags['license'][0]) !== $license)
        {
            $message = 'Invalid content found for @license tag, use ' 
                     . '"' . $license . '".';
            $phpcsFile->addError($message, $tags['license'][1]);            
        }
    }
}
an class="hl opt">("couldn't open tty for syslog -- still using /tmp/syslog\n"); /* now open the syslog socket */ // ############# LINUX 2.4 /dev/log IS BUGGED! --> apparently the syslogs can't reach me, and it's full up after a while // sockaddr.sun_family = AF_UNIX; // strncpy(sockaddr.sun_path, "/dev/log", UNIX_PATH_MAX); // sock = socket(AF_UNIX, SOCK_STREAM, 0); // if (sock < 0) { // printf("error creating socket: %d\n", errno); // sleep(5); // } // // print_str_init(log, "] got socket\n"); // if (bind(sock, (struct sockaddr *) &sockaddr, sizeof(sockaddr.sun_family) + strlen(sockaddr.sun_path))) { // print_str_init(log, "] bind error: "); // print_int_init(log, errno); // print_str_init(log, "\n"); // sleep(// } // // print_str_init(log, "] bound socket\n"); // chmod("/dev/log", 0666); // if (listen(sock, 5)) { // print_str_init(log, "] listen error: "); // print_int_init(log, errno); // print_str_init(log, "\n"); // sleep(5); // } /* disable on-console syslog output */ syslog(8, NULL, 1); print_str_init(log, "] kernel/system logger ok\n"); FD_ZERO(&unixs); while (1) { memcpy(&readset, &unixs, sizeof(unixs)); if (sock >= 0) FD_SET(sock, &readset); FD_SET(in, &readset); i = select(20, &readset, NULL, NULL, NULL); if (i <= 0) continue; /* has /proc/kmsg things to tell us? */ if (FD_ISSET(in, &readset)) { i = read(in, buf, sizeof(buf)); if (i > 0) { if (out >= 0) write(out, buf, i); write(log, buf, i); } } /* examine some fd's in the hope to find some syslog outputs from programs */ for (readfd = 0; readfd < 20; ++readfd) { if (FD_ISSET(readfd, &readset) && FD_ISSET(readfd, &unixs)) { i = read(readfd, buf, sizeof(buf)); if (i > 0) { /* grep out the output of RPM telling that it installed/removed some packages */ if (!strstr(buf, "mdk installed") && !strstr(buf, "mdk removed")) { if (out >= 0) write(out, buf, i); write(log, buf, i); } } else if (i == 0) { /* socket closed */ close(readfd); FD_CLR(readfd, &unixs); } } } /* the socket has moved, new stuff to do */ if (sock >= 0 && FD_ISSET(sock, &readset)) { s = sizeof(sockaddr); readfd = accept(sock, (struct sockaddr *) &sockaddr, &s); if (readfd < 0) { char * msg_error = "] error in accept\n"; if (out >= 0) write(out, msg_error, strlen(msg_error)); write(log, msg_error, strlen(msg_error)); close(sock); sock = -1; } else FD_SET(readfd, &unixs); } } } #define LOOP_CLR_FD 0x4C01 void del_loop(char *device) { int fd; if ((fd = open(device, O_RDONLY, 0)) < 0) return; if (ioctl(fd, LOOP_CLR_FD, 0) < 0) { close(fd); return; } printf("\t%s\n", device); close(fd); } struct filesystem { char * dev; char * name; char * fs; int mounted; }; /* attempt to unmount all filesystems in /proc/mounts */ void unmount_filesystems(void) { int fd, size; char buf[65535]; /* this should be big enough */ char *p; struct filesystem fs[500]; int numfs = 0; int i, nb; printf("unmounting filesystems...\n"); fd = open("/proc/mounts", O_RDONLY, 0); if (fd < 1) { print_error("failed to open /proc/mounts"); sleep(2); return; } size = read(fd, buf, sizeof(buf) - 1); buf[size] = '\0'; close(fd); p = buf; while (*p) { fs[numfs].mounted = 1; fs[numfs].dev = p; while (*p != ' ') p++; *p++ = '\0'; fs[numfs].name = p; while (*p != ' ') p++; *p++ = '\0'; fs[numfs].fs = p; while (*p != ' ') p++; *p++ = '\0'; while (*p != '\n') p++; p++; if (strcmp(fs[numfs].name, "/") && strcmp(fs[numfs].name, "/dev") && strncmp(fs[numfs].name, "/proc", 5)) numfs++; } /* Pixel's ultra-optimized sorting algorithm: multiple passes trying to umount everything until nothing moves anymore (a.k.a holy shotgun method) */ do { nb = 0; for (i = 0; i < numfs; i++) { /*printf("trying with %s\n", fs[i].name);*/ if (fs[i].mounted && umount(fs[i].name) == 0) { printf("\t%s\n", fs[i].name); if (strstr(fs[i].dev, "loop")) del_loop(fs[i].dev); fs[i].mounted = 0; nb++; } } } while (nb); for (i = nb = 0; i < numfs; i++) if (fs[i].mounted) { printf("\tumount failed: %s\n", fs[i].name); if (strcmp(fs[i].fs, "ext2") == 0) nb++; /* don't count not-ext2 umount failed */ } #ifdef MANDRAKE_MOVE fd = open("/dev/cdrom", O_RDONLY|O_NONBLOCK, 0); if (fd > 0) { printf("ejecting cdrom...\n"); ioctl(fd, CDROMEJECT, 0); close(fd); } #endif if (nb) { printf("failed to umount some filesystems\n"); select(0, NULL, NULL, NULL, NULL); } } #define BMAGIC_HARD 0x89ABCDEF #define BMAGIC_SOFT 0 #define BMAGIC_REBOOT 0x01234567 #define BMAGIC_HALT 0xCDEF0123 #define BMAGIC_POWEROFF 0x4321FEDC int reboot_magic = BMAGIC_REBOOT; int in_reboot(void) { int fd; if ((fd = open("/var/run/rebootctl", O_RDONLY, 0)) > 0) { char buf[100]; int i = read(fd, buf, sizeof(buf)); close(fd); if (strstr(buf, "halt")) reboot_magic = BMAGIC_HALT; return i > 0; } return 0; } int exit_value_proceed = 66; int main(int argc __attribute__ ((unused)), char **argv __attribute__ ((unused))) { pid_t installpid, childpid; int wait_status; int fd; int abnormal_termination = 0; int end_stage2 = 0; /* getpid() != 1 should work, by linuxrc tends to get a larger pid */ testing = (getpid() > 50); if (!testing) { /* turn off screen blanking */ printf("\033[9;0]"); printf("\033[8]"); } else printf("*** TESTING MODE *** (pid is %d)\n", getpid()); if (!testing) { mkdir("/proc", 0755); if (mount("/proc", "/proc", "proc", 0, NULL)) fatal_error("Unable to mount proc filesystem"); } /* ignore Control-C and keyboard stop signals */ signal(SIGINT, SIG_IGN); signal(SIGTSTP, SIG_IGN); /* disallow Ctrl Alt Del to reboot */ reboot(0xfee1dead, 672274793, BMAGIC_SOFT); if (!testing) { fd = open("/dev/console", O_RDWR, 0); if (fd < 0) fatal_error("failed to open /dev/console"); dup2(fd, 0); dup2(fd, 1); dup2(fd, 2); close(fd); } /* I set me up as session leader (probably not necessary?) */ setsid(); // if (ioctl(0, TIOCSCTTY, NULL)) // print_error("could not set new controlling tty"); if (!testing) { char my_hostname[] = "localhost.localdomain"; sethostname(my_hostname, sizeof(my_hostname)); /* the default domainname (as of 2.0.35) is "(none)", which confuses glibc */ setdomainname("", 0); } if (!testing) doklog(); /* Go into normal init mode - keep going, and then do a orderly shutdown when: 1) install exits 2) we receive a SIGHUP */ if (!(installpid = fork())) { /* child */ char * child_argv[2]; child_argv[0] = BINARY; child_argv[1] = NULL; execve(child_argv[0], child_argv, env); printf("error in exec of %s :-( [%d]\n", BINARY, errno); return 0; } while (!end_stage2) { childpid = wait4(-1, &wait_status, 0, NULL); if (childpid == installpid) end_stage2 = 1; } /* allow Ctrl Alt Del to reboot */ reboot(0xfee1dead, 672274793, BMAGIC_HARD); if (in_reboot()) { // any exitcode is valid if we're in_reboot } else if (!WIFEXITED(wait_status) || (WEXITSTATUS(wait_status) != 0 && WEXITSTATUS(wait_status) != exit_value_proceed)) { printf("exited abnormally :-( "); if (WIFSIGNALED(wait_status)) printf("-- received signal %d", WTERMSIG(wait_status)); printf("\n"); abnormal_termination = 1; } else if (WIFEXITED(wait_status) && WEXITSTATUS(wait_status) == exit_value_proceed) { kill(klog_pid, 9); printf("proceeding, please wait...\n"); return 0; } if (!abnormal_termination) { int i; for (i=0; i<50; i++) printf("\n"); /* cleanup startkde messages */ } if (testing) return 0; sync(); sync(); sleep(2); printf("sending termination signals..."); kill(-1, 15); sleep(2); printf("done\n"); printf("sending kill signals..."); kill(-1, 9); sleep(2); printf("done\n"); unmount_filesystems(); sync(); sync(); if (!abnormal_termination) { if (reboot_magic == BMAGIC_REBOOT) { printf("automatic reboot in 10 seconds\n"); sleep(10); reboot(0xfee1dead, 672274793, reboot_magic); } else { printf("you can safely turn your computer off\n"); /* if I ask kernel to do BMAGIC_POWEROFF, it panics :( */ } } else { printf("you may safely reboot or halt your system\n"); } select(0, NULL, NULL, NULL, NULL); return 0; }